3. 1. Introduction
• Through this article I would like to provide some Git commands for people in a
hurry and for all those who are too busy but seek a conduit to git.
Copyright @ Aishwarya R 1
5. 2.3. Using git in existing git repository
$ cd existing_repo
$ git remote add new-origin https://gitlab.com/aishwarya/git-
practice.git
$ git push -u new-origin --all
$ git push -u new-origin --tags
Copyright @ Aishwarya R 3
6. 3. Making Commits
• To know the working tree status and untracked files
$ git status
• Adding untracked files to working tree
$ git add filename
• Adding all the untracked files
$ git add .
• Commit the changes
$ git commit -a -m "Initial commit"
-a - to add modified files
Copyright @ Aishwarya R 4
7. 4. Editing Commits
• Changing the last commit message
$ git commit --amend
• Removing the last commit
$ git reset HEAD~
• Removing number of commits from HEAD
$ git reset HEAD~(N-1)
N - Number of commits, N-1 because index is starts with 0
Example, git reset HEAD~5
for discarding last 6 commits
Copyright @ Aishwarya R 5
8. • Undoing the commit
$ git revert commit_id
• To view the list of commits
$ git log
• To view the content of particular commit
$ git show commit_id
• To view the history of particular file
$ git log -- filename
Copyright @ Aishwarya R 6
9. 5. Working with Branches
• Creating new branch
$ git checkout -b branch_name
• To view list of all branches
$ git branch -a
• Renaming the branch name
$ git branch -m old_name new_name
• Cloning the specific branch
$ git clone --branch branch_name https://gitlab.com/aishwarya/git-
practice.git
• Removing branch
$ git branch -d branch_name
Copyright @ Aishwarya R 7
10. 6. Merging
• Move to the branch where you want to add changes
$ git checkout branch (where you want to add changes)
• Merging the merging_branch to the current branch
$ git merge merging_branch
• To get an overview of merge state where unmerged paths are files with merge
conflicts.
$ git status
• To view the conflicts in detail
$ git diff
Copyright @ Aishwarya R 8
11. 7. Applying Patches
There may be a situation occurs where you want to apply some of your commits to
another working tree. In that case, you can take every commit as patches from your
working tree and apply it on another working tree.
• Taking patch for particular commits
$ git format-patch -1 commit_id
• Taking patch for N number of commits from HEAD or commit id
$ git format-patch -N HEAD/commit_id -o ~/patches/
-o option to specify the output directory
• Checking the patch before apply
$ git apply --check --ignore-space-change --ignore-whitespace
~/patches/0001-patch1.patch
Copyright @ Aishwarya R 9
12. • Apply patch
$ git apply --ignore-space-change --ignore-whitespace
~/patches/0001-patch1.patch
• If patch does not apply cleanly, apply part of the patch
$ git apply --reject --ignore-space-change --ignore-whitespace
~/patches/0001-patch1.patch
• Apply patch with commit information like author and commit message.
$ git am --reject --ignore-space-change --ignore-whitespace
~/patches/0001-patch1.patch
• git am uses git apply behind the scenes, but does more work before (reading
commit logs) and after (creating commits).
• Removing patch from the working tree
$ git apply -R ~/patches/0001-patch1.patch
Copyright @ Aishwarya R 10