Software >> Services >> Version Control Systems >> git >> git cheatsheet

 

git commit -a

Stages files automatically

git log -p

Produces patch text

git log --graph --oneline This shows a summarized view of the commit history for a repo.

git show

Shows various objects

git diff

Is similar to the Linux `diff` command, and can show the differences in various commits

git diff --staged

An alias to --cached, this will show all staged files compared to the named commit

git add -p

Allows a user to interactively review patches to add to the current commit

git mv

Similar to the Linux `mv` command, this moves a file

git rm

Similar to the Linux `rm` command, this deletes, or removes a file

git checkout

Switch branches or restore working tree files

E.g. of restoring working tree files - Discard recent changes to myfile (after they have been staged and mistakes made in the modifications)

git checkout myfile

E.g. switch to newbranch

git branch newbranch
git checkout newbranch

E.g. create new branch and switch to it

git checkout -b even-newerbranch
 

git commit --amend Replace the tip of the current branch by creating a new commit.

Amend the previous commit.  Rewrites the git history removing the previous commit and replacing it with the amended one.  You can also update the commit message
git reset

Reset current HEAD to the specified state

e.g. Move myfile back to untracked status (e.g. it was tracked by mistake by git add *)

git reset HEAD myfile
 

HEAD => last commit
HEAD^ => 2nd last commit
HEAD~2 => 3rd last commit
 

git revert

Revert some existing commits

e.g. to revert to 2nd latest commit

git log -2

## note commit 2nd latest commit id

git revert commit_id

git branch

Used to manage branches

eg. Create new branch with name <name>

git branch <name> :

 

eg. Delete branch with name <name>

git branch -d <name>

 

eg. Forcibly delete branch with name <name>

git branch -D <name>
 

git merge

 

eg. git merge <branch>

e.g. git merge --abort

 

git clone URL Git clone is used to clone a remote repository into a local workspace


Using HTTPS

e.g.

git clone https://github.com/youraccount/yourrepo


Using SSH with public key

If we have already created SSH key-pair and added your SSH public key to your GitHub Account, then

git clone git@github.com:youraccount/yourrepo

 

git push

Git push is used to push commits from your local repo to a remote repo
git pull Git pull is used to fetch the newest updates from a remote repository
git remote

List remote repos

e.g. list remote repos verbosely

git remote -v

git remote show <name> Describes a single remote repo
git remote update Fetches the most up-to-date objects
git fetch Downloads specific objects
git branch -r

List remote branches; can be combined with other branch arguments to manage remote branches