Git Lessons

This post is about some Git commands that I have learnt over time. I’ll add more as I learn more. 🙂

git init

To create a new git repository.

git add <your-file-name>

To add your-file to the repository, better say adding the file to the index. Sometimes also used when merging creates some conflicts.

git add .

It looks at the working tree and adds all those to the staged changes if they are either changed or are new and not ignored. It does not stange any remove actions.

git add -u

It looks at all the currently tracked files and stages the changes to those files if they are different or if they have been removed. It does not add any new files, it only stages changes to already tracked files.

git add -A

To add all the changed files in your current git repo. It does both

git add .

git add -u

git commit -m <‘your-commit-message’>

This commits <your-file> to HEAD, of your local working copy, but not to the remote repository yet.

git rm <file_to_remove>

To remove the file from the index, or from the working tree and index.

git remote add origin <(ssh|https)path-to-your-git-repo>

To connect your repository to the remote server.  Now you are ready to push the changes to the selected remote server.

git push origin master

To send the changes in the HEAD of your local working copy to your remore repository.

git clone /path/to/the/repository

To create a copy of a repository on your local machine.

git checkout -b <branch_name>

To create a new branch (other than master) and swtich to it.

 git push origin <branch_name>

To push your new branch to the remote server. Unless you push it, it won’t be available to others.

git checkout master

To switch back to master.

git branch -d <branch_name>

To delete the branch again.

git pull

To update your repository to newest commt.  This fetches and merges remote changes.

git merge <branch_name>

To merge <some_other_branch> to your active branch.

git log

To study repository history.

git config color.ui true

To use colorful git output.

git status

To view the status of your files in the working directory and staging area.

git commit –amend -m <“New_Commit_Message”>

To change the messge of your most recent commit, which is not yet pushed.

git format-patch -1 <sha>

Generate a patch for a specific commit.

git reset <file>

To undo git add <file>

git commit -s -v

-s for add Signed-off-by line by the committer at the end of commit log message.

-v to  show unified diff between the HEAD commit and what would be committed at the bottom of the commit message template.

git show

To see  the changes introduced by the most recent commit, uses HEAD by default.

git show HEAD~~ or git show ~2

To refer to the commit before HEAD.

git rev-parse HEAD

To get the commit id of HEAD.

git log –pretty=oneline –abbrev-commit

As it says, it gives pretty one line commit logs.

git format-patch –cover-letter -n –thread-shallow –cc=”” <intialSHA>^..<endingSHA>

This will generate a patch set having a cover letter, from the intialSHA to endingSHA.

–cc option let’s you add the mail id to all patches generated and hence reduce your work to add individually in all.

git rebase -i <SHA>^

It allows you to rebase your commit starting from <SHA>.

git commit –amend -v

This allows you to edit your commit message when rebasing.

git rebase –continue

To continue rebase.

git rebase –abort

To abort the rebase.

git checkout .

To checkout all local changes in a branch.

git mergetool

If some conflicts occur while merging or rebasing.

git checkout –<filename>

To checkout changes in the <file>.

git reflog

To view a log of where your HEAD and branch references have been for the last few months.

git merge –abort or git reset –merge

To reset a merge.

git diff

To have a diff stat of what changed. Shows only unstaged changes.

git diff –cached

To see the changes to be committed, staged changes.

git add -p 

Add parts of file to the staging area.

git format-patch –subject-prefix=”Some String”

To put a subject prefix to the patches.

git log –author=<username>

To list all commits by a specific author.

Have a ❤ look.


4 thoughts on “Git Lessons

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s