GIT: Mixed Topics


Table of Contents

GIT Installation

Linux:

$ sudo apt update

$ sudo apt install git

Windows:

Go to GIT’s official page https://git-scm.com, and download and install the GIT client. This will install GIT with its own terminal. You run the GIT commands inside that special git terminal.

This download will also install a simple graphical UI for GIT which is called “Git GUI”:

You can also install GIT in such a way that it can be used from PowerShell but that’s a bit more complicated, so I prefer the easier option.

Git configurations

Commit parameter

Defining the author

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)

With the following commands, you can configure the author name and email address globally or per repository

Globally:

$ git config --global user.name "John Doe"

$ git config --global user.email john@doe.org

Per repository:

$ git config user.name "John Doe"

$ git config user.email john@doe.org

Showing the author data:

git config user.name

git config user.email

Storing credentials

If you use git commands on a system where you are not logged in with a user that is automatically authenticated to the service that is providing the remote repository then you might be prompted for some credentials each time you try to access the remote repository. One easy but unsafe way to cause git to stop promoting you for credentials is to use the following command, which tells git to store your credentials the next time you are prompted and enter it:

git config --global credential.helper store

This will cause git to store your credentials including your password the next time you enter them in a file (located in your home directory, at ~/.git-credentials) on the system in plain text, which is a security risk. So use this approach only if you are not worried about your password being visible to anybody who has access to your home directory.

A more secure and better solution would be to use a more secure credential helper. For example, on a Windows system, you could try to use the wincred credential helper.

Resetting the repository

Reset the repository to a specific commit

With the following command, we can revert all local changes and set the repository to the state given a specific commit (But be aware this is a dangerous command which can permanently delete your local changes, better backup everything before using such commands!):

git reset --hard commit_sha_1_number

Creating patches from commits

Creating a patch from one commit

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)

Sometimes we would like to have a patch file for changes that were included in one specific file. This can be achieved with the following command:

git format-patch -1 commit_sha_1_number

Applying a patch to your local copy of the repository

A patch can be applied to your local repository with the following command:

git apply path_to_patch_file

Correct / Revert commits

Reverting a specific commit

git revert bdd68fd1

Corrections after a bad commit

Changing the commit text

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)

Case: Commit was not pushed yet

You can change the commit text of your last not yet pushed commit with the following command:

git commit --amend -m "Corrected commit message."

Preventing unwanted files from being added to the repository

There are two ways that unwanted files end up remaining in the repository:

  1. The affected files are new files that are not added to the list of files to be ignored and therefore they are tracked.
  2. The affected files are already in the repository and therefore tracked regardless if they have been added to the list of ignored files or not.

Problem 1

To take care of the first problem we have to simply add the affected file(s) or folder into the ignore list (edit the .gitignore file).

Problem 2

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)

The following command takes care of the second problem:

In case of a single file:

git rm --cached filePath

In case of a folder

git rm -r --cached folderPath

Adding/Reverting changes

Adding files to the repository

Adding to stage

When you make changes to files in your local working copy, you can add all affected files with the git add command to the staging area:

git add *

With the git status command, you can see the files currently in the staging area ready to be committed:

git status

Removing from stage

To unstage a specific file use the following command:

git reset HEAD – path_to_file

To unstage all files use the following command:

git reset HEAD -- .

Committing

With git commit command you can then send the changes from the staging area to your local repository:

git commit a-message-about-the-commit

Removing files from the repository

Removing from the repository and locally

git rm somefile.txt

Removing from the repository but keeping it locally

Remove the file from the repository with “git rm –cached” command, this won’t delete the file locally but only from the repository:

git rm -–cached somefile.txt

Commit and push your changes:

git commit -m "Removed from repository"

git push

Working copy changes

Saving your current working copy changes

By git stash

git stash with name

Command to save a git stash with a name:

git stash push -m "say-my-name"

Command to restore a git stash with a name:

git stash apply stash^{/say-my-name}

By patch

Command to save your working copy changes

git diff > some_name.patch

Command to restore your working copy changes:

git apply some_name.patch

Dropping your working copy changes

The following commands will drop your local working copy changes which have not been committed yet:

git checkout .

Or the following command for newer GIT versions:

git restore .

Repository

Create a new repository

If you have a folder with some files in it and you want to create a repository containing the files in that folder you can proceed as follows:

First, you can initialize a repository inside that folder with the following command:

git init

Before adding files to your newly created repository you should create the .gitignore file in the root of your folder and add files and folders which should be excluded from the repository.

Here is an example of a .gitignore file:

# Build directories
build/
ios/Flutter/

# IDE configuration files
.idea/

# Dependency directories
android/build/
ios/Pods/

# Compiled code
*.class
*.jar
*.aar
*.apk

# Credentials and secrets
*.keystore
*.jks
*.pem
*.p12
*.pfx
*.cer
*.key
*.pass

Often you have also a command available that will generate such a default .gitignore file for your specific tool. For example in case of C# projects you can have a default .gitignore file be automatically generated with the following command:

dotnet new gitignore

Now that you defined which files/folders you prefer not to be added to the repository, use the following command to add all the other files to the staging area:

git add .

Then you can commit all the files in the staging area with the following command to the repository:

git commit -m "Initial commit"

Update repository

Update without local changes

The git fetch command updates your locally stored information about your remote repository:

git fetch

Update with local changes

The git pull command updates your locally stored information about your remote repository but goes further and will try to merge new remote changes into your local repository. So git pull is more invasive than git fetch:

git pull

Branches

Create a new branch

The checkout command creates a new branch from the master branch and at the same time switches from the master branch into the newly created branch:

git checkout -b new-branch

Checkout remote branch to local

In case you have some remote branches which you miss on local. You can enter the following command to see all remote branches available for checkout:

git branch -a

This lists all remote branches. The ones that start with remote/* are the ones which you miss on local and which you can check out to local

To check such a remote branch to local you can use the following command:

git switch branchname

Deleting branches

git branch -D somebranch

Cleaning up branches

Cleaning up branches from the origin

Branches that no longer exist remotely

Sometimes you end up having a branch existing under origin (which you can see by using the “git branch -r” command) which no longer exists remotely. This would for example prevent you from starting a branch with the same name locally. To clean up such branches from the origin you can use the following command:

git remote prune origin

or

git fetch –prune

Cleaning up branches from local

Branches that have been merged to master/main remotely

Make sure that your local repository has the latest remote changes:

git fetch

Check the situation before cleanup

git branch

Cleanup command

git branch -vv | grep ': gone]' | grep -v '\*' | awk '{ print $1; }' | xargs -r git branch -d

Check the result after cleanup

git branch

Merging one branch to another branch

With the git command in the ubuntu terminal

Switch to the branch where you want to merge into:

git switch merge-target-branch
git merge merge-source-branch


This will open the GNU nano editor which allows entering a message for the merge commit.

The GNU nano editor has options at the bottom, which represent commands. To execute each of those commands you have to use the [ctrl] key + the letter for the command.
To write the commit message, you have to press [ctrl] + o and then [ctrl] + x to exit from the nano editor.

Restoring branches

Restoring accidentally deleted local branch

The following command shows how to restore a local branch, for example, a branch which was delete accidentally, in this case, the master branch:

git branch master origin/master

Other branch operations

Checkout remote branch to a local repository which did not start the branch

Sometimes you or someone else might have created a new branch on local repository A and then you want to check out that same branch on your local repository B. To do this you cannot just execute

git checkout somebranch

because the above command would create a new branch. But you want to have the branch you or someone else has already created on another local repository.

In this case, you have to first pull all changes from remote on local repository B.

git pull

Then you should see the branch you would like to have on your local repository B with the following command on local repository B:

git branch -r

...
origin/somebranch
...

Then you can copy that to your local repository using the following command:

git branch somebranch origin/somebranch

After that, you can switch to the copies local branch using the following command

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)
:

git checkout somebranch

Find branches a specific commit is on

The following command will tell you all branches which have the given commit in their history. Of course, this is less useful if the affected commit has already been merged because then its original branch wouldn’t exist anymore.

git branch -a --contains

Find out the original branch from which a given branch was created

There is no command that can tell you which branch your branch was created from, because GIT does not store information about branches and only cares about commits. You can read more about this here:

https://www.reddit.com/r/git/comments/wllb2e/how_to_know_the_branch_s_parent_name/

As explained by the discussion above, there are some simple ways to find the parent branch:

If your branch is freshly (within 90 days) created from the original branch, you can use the following command to quickly see the “parent” branch name:

git reflog

The “git reflog” command is used to display a log of changes to the local repository’s refs. It shows a list of commits that have been made to the repository, along with the names of the branches that were updated. Because the information of branches is available, you can use this command to see from which branch your current branch was branched out.

Another option is to show the commits graph:

git log --graph --oneline --decorate

This shows a graph where you can see visually from which branch the current branch was branched out.

Miscellaneous

Ignoring the effects of chmod command on Linux

Sometimes when you are working in Linux environment you might use the chmod 777 command because you want to make your scripts executable. If you do that, then GIT might consider all affected files as changed and include them in the list of changed files even though you have not changed any contents.

With the following command, you can make GIT ignore those chmod 777 changes:

git config core.fileMode false

But be aware that this is considered not a good practice, especially because you often apply chmod 777 to files that are not supposed to be executable, probably out of laziness. You can find more information in the following discussion about this topic:

https://stackoverflow.com/questions/1580596/how-do-i-make-git-ignore-file-mode-chmod-changes

Showing the commit history

The git log command lists the commits made in your repository in reverse chronological order:

git log

Troubleshooting

merge: … – not something we can merge

Problem

We try to merge the branch abc/xyz with the current branch using the following command:

git merge abc/xyz

Which issues the following error:

merge: abc/xyz - not something we can merge

Potential resolution

Make sure that you have checked out the branch abc/xyz locally before running the git merge command.