Wednesday, June 16, 2021

Git tips and tricks


See status of the repository:

    git status

See status in certain directory:

    git status <path to directory>

See status in the current directory:

    git status .



Jumps to another branch:

    git checkout <branch name>

Rename branch:

    git branch -m <old name> <new name>

See what branches are in your repository:

    git branch


Creates new branch and jumps to it:

    git checkout -b <branch name>

Create a branch from mainline:

    git branch <new branch name> mainline

Create a branch from a specific commit:

    git branch <new branch name> <commit name>

Remote repository branches

To create a branch in the remote repository, need to explicitly push (the -u sets local branch to track the remote branch):

    git push -u origin <local branch name>:<new remote branch name>

Push the newly created local branch to repository:

    git push -u origin <local branch name>


Delete a local branch:

    git branch -d <branch to delete>

Delete a remote branch. When you delete a remote branch, you are in effect, pushing a null commit to the server.

    git push origin :<branch to delete> 

    git push origin --delete <branch to delete>


Merge changes from one branch into another

    git merge <branch to merge from> 

Resolve the conflict

    Open the file and make any necessary changes

    Use the git add a command to stage the new merged content

    Create a new commit with the help of the git commit command

    Git will create a new merge commit to finalize the merge

Hardcoded Red Neck Way for Resolving Conflicts

    After the merge, the worse thing git does is it makes changes in the file per se. So it is easy to mess up everything. Paying attention to that git adds additional comments to the file. Sometimes after resolving the merge nothing works. Debug fails because the new "resolved" file looks like neither of the 2 previous. Moreover, it can contain new errors for debug. 

    Let's say we have conflicts only on one or two files. Than it is easy to hardcode the merge conflict. Say the conflict in the file is between its previous version A and version B. Say we know that we want to keep version A because we are sure that it works stable. Then we just copy and paste all the version A file to the separate folder on a disk out of the git control and call it copyOfVersionA. We can get that version A from one of previous commits (git checkout <commit_name>). 

    After git flags a merge, we just replace the current file in the local repository with the copyOfVersionA that we created earlier. Then add and commit. Merge is resolved.

Abort in case of disaster

    git merge --abort

We can always go back to the safe state with this command.


This is to transfer commits between branches. To transfer commits made at another branch: Cherry picking in git means to choose a commit from one branch and apply it onto another. Make sure you are on the branch you want to apply the commit to.

    git checkout <branch-to-apply-commit-to>

    git cherry-pick <commit-hash>

Abort cherry-pick in case of disaster:

    git cherry-pick --abort


Make a commit

    git commit -m "<description>"

Temporary switch to another commit

    git checkout commit_hash

Switch back to the latest commit in the branch

    [or] git checkout [branchname]


Differences between branches:

    git diff <branch1>..<branch2>

Differences between a remote branch called origin/mainline, and the local mainline branch:

    git diff origin/mainline..mainline

Changes between two commits:

    git diff <commit1> <commit2>

Changes in certain commit

    git show <commit>   

Staged changes diff

    git diff --all

Show names of changed files, status (deletion/removal), number of lines changed:

    git diff --stat

Show only names of changed files and addition/deletion status:

    git diff --name-status

Show only names of changed files and addition/deletion status

    git diff --summary

Show only names of changed files

    git diff --name-only

Ignore temporary files

1. Go to package folder

2. vim .git/info/exclude

3. In the end of the file, write: *~

The same mask can be applied for other types of files


See a history of the commits on the current branch

    git log

See also difference for every commit listed

    git log -patch (or git log -p)

See only latest number of commits

    git log -<number of commits to be listed> git log -3

See graph of branches

    git log --graph


Delete everything uncommitted

    git reset --hard

Abandons uncommitted changes of certain file to last commit

    git checkout -- <file_name>

Remove the last commit and delete all of the changes to the files intact

    git reset --hard HEAD~1

Remove the last commit but leave all the changes

    git reset --soft HEAD~1

Remove the last commit from the remote repository (i. e. github), undo last push (if the last push consisted of one commit):

    git push -f origin HEAD^:master

No comments:

Post a Comment