First steps with git: cheat-sheet.


Create a new git repository.
git init               # create a new repo from scratch.
git clone <repo URL>   # clone an existing remote repository.


Add and remove content from the index (i.e, staging).
git add <file or directory>   # stage a file or directory.
git add -A                    # stage all files in the project's directory.
git add -u                    # stage all already tracked files.

git rm <file>                 # delete file from both working dir and index.
git rm --cached <file>        # remove file from index.

git reset HEAD <file>         # remove newly staged content of a specific file 
                              # from the index.
git reset HEAD                # remove all newly staged content from index.


echo "file pattern" >> .gitignore          # add file pattern to the shared 
                                           # .gitignore list.
echo "file pattern" >> .git/info/exclude   # add file pattern to the private 
                                           # ignore list.


Make a new commit.
git commit -m "commit message"         # make a new commit.
git commit -m "commit message" <file>  # shortcut: add <file> to index + commit.
git commit -am "commit message"        # shortcut: add all already tracked files 
                                       # to the index and commit.


Display file status, content differences and history.
git status                       # show status of files in the working directory.
git show                         # show latest commit on current branch.
git show <commit ref>            # show content of commit <commit ref>.

git diff                         # show differences between working tree and 
                                 # the index.
git diff --cached                # show differences between the index and the 
                                 # current HEAD position.
git diff <commit 1> <commit 2>   # show difference between commit 1 and 2.

git log                          # display history of the current branch.
git log --oneline                # condensed display of current branch history.
git log --all --decorate --oneline --graph   # display entire history of repo.

git ls-files                     # list files in the index (i.e. tracked files).


Branches.
git branch                     # list local branches.
git branch -a                  # list local and remote branches.
git branch <new branch>        # create a branch named <new branch>.
git checkout <branch name>     # switch to branch.
git checkout -b <new branch>   # shortcut: create <new branch> and switch to it.


Merge, rebase and cherry-pick.
git merge <branch name>            # merge <branch name> into the current branch.
git rebase <branch to rebase on>   # rebase the current branch onto the latest 
                                   # commit of <branch to rebase on>
git cherry-pick <commit ref>       # cherry-pick commit <commit ref> onto the 
                                   # current HEAD.


Retrieve content from git repo.
git checkout <commit ref> <file>  # retrieve the specified <file> from commit 
                                  # <commit ref>. Warning: this will replace the 
                                  # current version of <file> in the working tree
git checkout <commit ref>         # Set entire working tree content to its state 
                                  # as recorded in commit <commit ref>. Note 
                                  # that after this command you will be in 
                                  # "detached HEAD" mode.
git checkout -b <commit ref>      # Create a new branch starting at commit 
                                  # <commit ref>.


Working with remotes.
git fetch                        # fetch all changes of the remote that you 
                                 # don’t have yet in your local repo. Does not 
                                 # modify anything in the working directory.
git <branch> -u origin/<branch>  # create a new local branch that tracks the 
                                 # upstream branch origin/<branch>.
git pull                         # fetch + merge all changes into current branch.
git push -u origin <branch>      # set origin/<branch> as the upstream for the 
                                 # current branch + push new content on the 
                                 # current branch to the remote.
git push                         # push new content on current branch to remote.


Relative references (example with HEAD).
HEAD~     # parent of current HEAD.
HEAD~2    # grand-parent of current HEAD.
HEAD^1    # first direct parent of HEAD.
HEAD^2    # second direct parent of HEAD.


Configuring git.
# create a new git command alias named <alias name>
git config --global alias.<alias name> "git command to alias"  

# set user name/email globally (for all git projects).
git config --global user.name <user name>   
git config --global user.email <email>      

# set a user name/email only for the current git repo.
git config user.name <user name>
git config user.email <user email>

# retrieve global user name/email.
git config --global --get user.name         
git config --global --get user.email        


Last modified: Monday, 2 March 2020, 6:27 PM