git config is used to create and modify system/user/repo git configuration file.
Options:
--system: will set the config system wide. for all user’s all repos in the system. System wide git config file is stored generally in /etc/gitconfig.
--global: will set the config user wide. For a specific user’s all git repos. stored in ~/.config/git/config or user’s home directory.
--local: will set the config for a specific repo. Stored in .git/config.
--list: show all the list of current config variables and their values.
Examples:
1 2 3 4 5
git config --global user.name 'singsong' git config --global user.email '[email protected]' git config --global core.editor vim git config user.name # will show value of user.name git config user.email # will show value of user.email
Initializing a project
git init is used to initialize a project as git repository.
1
git init
Cloning a repository from remote
git clone is used for cloning a remote repository into local system.
git status is used to see status of the project files. Untracked/Tracked/Modified/Staged.
1 2
git status # Show details status git status -s # Show brief status
Adding files to staging area
git add is used to add untracked/modified files to staging area and prepare for commit. If a directory is added, its contents will be added recursively.
Adding whole file:
1
git add README.md
Adding portion of a file:
1
git add -p README.md # This will bring an interactive prompt
Inspect changes of a file
git diff is used to show changes of a file
1 2 3
git diff # Shows changes between last staged and unstaged git diff --staged # Shows changes between the last commit and staged git diff COMMIT_HASH~ COMMIT_HASH # Show diff between last commit and its ancestor
Creating and applying patch file
For creating patch file use git diff:
1
git diff > mypatch.patch
If you want add untracked files too first stage them using git add, then create the patch file:
1
git diff --cached > mypatch.patch
Now you can remove those untracked files from staging area (See 1.13) if you don’t want to commit them.
If you want to add binary files in the patch:
1
git diff --binary > mypatch.patch
To apply a patch:
1
git apply mypatch.patch
Commiting
git commit is used for commiting changes that are staged
1 2 3 4
git commit # To commit changes that are staged. git commit -m "COMMIT_MSG"# Commit with inline commit message. git commit -a # stage all tracked modified files and commit. git commit -v # will add diff of changes in the commit message.
Removing files
git rm is used to remove files.
Following command will remove a file from tracking and also remove from system(HDD/SSD).
1
git rm project_file
If the file is already modified or staged, then to remove -f have to be used.
1
git rm -f project_file
To remove only from git but not from the system(HDD/SSD). So that git no longer track the file.
1
git rm --cached project_file
Moving/Renaming files
git mv is used to rename files/folders
1
git mv project_file my_project_file
View logs
git log is used to view commit logs/history
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
git log# Show entire commit log of the repo git log -n # Show last n commit logs git log -p -n # Show last n commit log including change/patch/diff in those commits git log --stat# Commit log witch list of modified files and stat of the changes in those files git log --pretty=oneline # show each commit log in one line with only full SHA and subject git log --pretty=short # Log with SHA, author and subject git log --pretty=full # Log with SHA, author, commiter, subject, message body git log --pretty=fuller # Log with SHA, author, commiter, subject, message body, write and commit dates git log --oneline # Show first 7 characters of SHA value, Heading git log --format="%h - %an, %ad: %s"# Custom log output format git log --graph # Will show a ascii graph of commit history git log --relative-date # Will show relative date(days, weeks etc) rather than exact date git log --since=2.weeks # Show all commits since a specific date or relative date git log --until=2.weeks # Show all commit before a specific date or relative date git log --author=AUTHOR # Show the logs of a specific author git log --follow -- FILE # Show all the commits to a specific file git log testing # Show the logs of testing branch git log --all # Show logs of all branches. By default shows only the logs of checked out branch
Modifying last commit
After a commit, to redo that commit for additional changes git commit --amend can be used. This is useful when accidentally forgot to add files to a commit.
1 2 3
git commit -m "Serious bugfix"# Accidentally commited without forgotten_files git add forgotten_files # forgotten_files is added git commit --amend # Previous commit will be modified by adding forgotten_files
If this command is used just after a commit, editor will show up to edit the commit message. So this can be used to modify minor changes in the latest commit message.
This will only change the local branch name. To change the remote branch name too you have do first push the renamed branch to remote:
1
git push --set-upstream origin good_branch_name
Now there will be two branch in remote remotes/origin/bad_branch_name and remotes/origin/good_branch_name.
Remove remote/origin/bad_branch_name
1
git push origin --delete bad_branch_name
Merging a branch to another branch
Let’s say you are now in testing branch. You have added some changes and now want to merge the commits of testing branch into master branch:
1 2
git checkout master git merge testing
Remote Branches
Remote branches are named like <remote_name>/<remote_branch_name>. For example origin/master. This points to the latest commit of your remote repository at the time of your last synchronization.
Fetching commits from remote repository
1
git fetch <remote_name>/<remote_branch_name>
This will synchronize the remote branch with the local repository. For example if your remote branch name is origin/master this will synchronize orgin/master with the local repository.
Note that this will not merge commits from remote branch(origin/master) into your local branch(master). You have to do it manually.
Pushing/fetching a new branch to/from remote repository
For example you have created a new branch named testing and added some commits to it. Now you want to upload it to your remote repository:
1
git push origin testing
This will create a new branch named testing in your remote repository. If you want to name the remote branch name different from the local branch name:
1
git push origin testing:experimental
For the next example lets say you didn’t name the differently. Now if someone who is also forked that remote repository do fetch, a new remote branch named origin/testing will be created in their local repository. Not this will not create any new local branch to work on for them. They will have to do it manually:
1
git checkout -b testing origin/testing
You can also use this shortcut if you keep the local branch name and remote branch name same:
1
git checkout testing
If you want to give a different name to the local branch:
1
git checkout -b experimental origin/testing
Tracking Branch
Checking out a local branch from a remote branch creates a tracking branch. Tracking branch means that local branch and the remote branch has a direct relationship with each other. If you are in that local branch when you do a push git will automatically push the commits to the remote branch and when you do pull git will automatically merge all the commits from the remote branch to that local branch.
For example in above example if you are in testing branch, when you do a pull git will merge all the commits from origin/testing to testing automatically. And after working on testing when you do push git will automatically push all the commits to origin/testing.
If you have a local branch which don’t have a tracking branch or you want to change the remote tracking branch. For example if you currently on testing branch and want to set origin/testing as you remote tracking branch:
1
git branch -u origin/testing
Pulling
For automatically fetching commits from remote branch and merging with local tracking branch use:
1
git pull
It is equivalent to:
1 2
git fetch git merge
Deleting a remote branch
1
git push origin --delete testing
This will delete branch testing from remote repository.
Cherry Picking
In Git, you can cherry-pick a commit (a set of changes) from an existing branch, and apply those changes to another branch. Cherry-picks can help you:
Backport bug fixes from the default branch to previous release branches.
Copy changes from a fork to the upstream repository.
For merging a specific commit or set of commits from a brach to another branch.
1 2 3 4 5 6 7 8 9
git checkout my-feature # ... changes to the working tree (your file system). git add . git commit -m "Minor tweaks."
git checkout master
git cherry-pick - git cherry-pick COMMIT_HASH
In the command above, the - refers to the most recent commit of the my-feature branch, whereas COMMIT_HASH is the hash of the commit you cherry picked from my-feature branch, these will create new commits to master branch.
For cherry picking a set of commits(A, B, C, and D are commit hash):
1
git cherry-pick A^..D
The command above assumes that A is an older commit than D. Also, it includes both extremities. What if you want to exclude A? In such case, you’d run this:
1
git cherry-pick A..D
Rewriting history
Removing commits
Lets say your commit history looks like this:
Number (newest-oldest)
Commit Hash
Commit Message
1
4d884d0
Important fix
2
5540e24
server fix
3
4c5199f
client fix
4
7879940
My awesome fix
5
2c27f34
minor fix
If you want to remove consecutive commits you can use git rebase. For example if you want to remove commit 3 and 4 you can do following:
1
git rebase --onto <branch_name>~<number of the oldest commit you want to remove> <branch_name>~<number of the oldest commit you want to keep> <branch_name>
So if you want to remove 3 and 4 in master branch:
1
git rebase --onto master~4 master~2 master
Now if you want to remove non-consecutive commits, you can use git cherry-pick. For example if you want to remove commit 2 and 4 you have to do following procedure:
Go to oldest usable commit(In this case 5)
Create a new branch
cherry pick the commits you want to keep(In this case 3 and 1) after that commit(In this case 5).
Checkout to the previous branch you were in.
Do a hard reset to the last usable commit(In this case 5)
Merge the new branch in this branch
So lets say in our case you were in master branch and want to remove commit 2 and 4: