[Git]: Commonly used git commands

git

11/07/2019


Basic git commands

add

TEXT
git add ${file name}

add all modified files

TEXT
git add .

commit with commit message

TEXT
git commit -m "${commit message}"

add all modified files & commit

TEXT
git commit -am "${commit message}"

add remote repository

TEXT
git remote add ${github remote repo address}

push on a branch

TEXT
git push origin ${branch name}

create a branch

TEXT
git checkout -b ${branch name}

move to different branch

TEXT
git checkout ${branch name}

change name of branch

TEXT
git branch -M ${new branch name}

display state of working directory

TEXT
git status

see differences with last commit

TEXT
git diff

display commits made in the repo

TEXT
git log

more information on this post

show commit log in a graph

TEXT
git log --graph

More commands

revert a modified file

TEXT
git checkout -- ${filename}

change the most recent commit message

TEXT
git commit --amend

When changing the most recent commit message that's already pushed, use the following

TEXT
git push --force-with-lease ${repository} ${branch}

${repository} and ${branch} are optional

change old commit message

TEXT
git rebase -i HEAD~${#commits to go back}

For example, HEAD~2 is the second last commit. It will display a list of the last 2 commit messages; bottom displays the most recent commit

TEXT
pick 86fbdbe modified post # 2nd MOST RECENT COMMIT
pick f4683d1 modified post # MOST RECENT COMMIT
# ...

Replace pick with reword then save & quit

TEXT
reword 86fbdbe modified post
pick f4683d1 modified post

It will open up the editor to change the specified commit with "reword". Modify the message then save&quit. Push with the following

TEXT
git push --force-with-lease ${repository} ${branch}

revert the most recent commit

TEXT
git revert HEAD
OR
git reset HEAD^

more information on this post

add remote repo as upstream on local repo

TEXT
git remote add --track master upstream ${github repo remote address}

update master branch to the latest version of upstream

TEXT
git pull --ff upstream master

combine multiple commits

TEXT
git rebase -i HEAD~${number of commits}

display git history

TEXT
git reflog

add the modified file to the most recent commit

TEXT
git commit -C HEAD --amend

ignore the modified file from the stage

TEXT
git update-index --assume-unchanged ${file name}

staging area is a step before the commit process.

revert ignoring the modified file from the stage

TEXT
git update-index --no-assume-unchanged ${file name}

stash the changes in dirty working directory away

TEXT
git stash

i.e. record the current state of working directory without local modification on current branch

remove a single stashed state and apply it on the current working state

TEXT
git pop

pick particular commit and merge it from other branch

TEXT
git cherry-pick ${commit hash number}

remove upstream

TEXT
git branch --unset-upstream

WRITTEN BY

Keeping a record