Git commands
commands.
Fri Apr 20 2018
GIT
Add, Commit, Pull, Push:
git status #Check status
# Add
git add file-name #Add a file to the staging area
git add -A #Add all new and changed files to the staging area
git add . #Adding all files changes in a directory
# Commit
git commit -m "commit message" #Commit changes
git commit filename -m 'commit message' #Add file and commit
# Pull
git pull #Update local repository to the newest commit
git pull origin [branch name]
git remote show origin #Show where 'origin' is pointing to and also tracked branches
git remote -v #Show where 'origin' is pointing to
# Push
git push origin [branch name] #Push a branch to your remote repository
git push -u origin [branch name] #Push changes to remote repository (and remember the branch)
git push #Push changes to remote repository (remembered branch)
git push --all --tags origin # Push all branches and tags
Branch
git branch
List branches (the asterisk denotes the current branch)
git branch -r
List remote branches
git branch -a #List all branches (local and remote)
git branch -r #List all branches (remote)
git branch [branch name] #Create a new branch
git checkout -b [branch name] #Create a new branch and switch to it
git checkout [branch name] #Switch to a branch
git checkout - #Switch to the branch last checked out
git checkout -- [file-name] #Discard changes to a file
Merge
git merge origin <branch-A> # Merge remote 'branch-A' with current branch
git merge-base HEAD origin/master # Find the base commit between two branches
git merge <from-commit> <to-commit> # Merge a range of commit (including two given commits)
Delete branch
git branch -d [branch name] #Delete a local branch.
git push origin --delete [branchName] #Delete a remote branch
git branch origin :<branch-name> #Delete a remote branch
//Delete a remote branch (old-name) and push a new local branch (new-branch).
git push origin :<old-name> <new-name>
Track a new branch.
git branch -u <remote/branch>
Checkout
git checkout - # Switch to the last branch you are
git checkout -b <branch-name> # Create & checkout to new branch
git checkout -b <branch> <remote/branch> # Create a new branch from a remote branch history
git checkout <commit-hash> # Go to a specific commit
git checkout <branch-name> # Return to present state
git checkout -- <filename1> <filename2> # Discard all changes of file1 and file2
Cherry Pick
git cherry-pick <commit-hash> # Copy a single commit to current branch
git cherry-pick <commit-hash> -X theirs # If conflicts occurs then accepts theirs
git cherry-pick --edit <commit-hash> # Pop-up a editor, then change the commit message.
git cherry-pick <hash> <hash> # pick multiple commits
git cherry-pick --signoff <commit-hash> # --signoff add current users name to commit message
diff
git diff #See all (non-staged) changes done to a local repo
git diff --cached # See all (staged) changes done to a local repo
git diff --stat origin/master #Check what the changes between the files you've committed and the live repo
git blame [filename] #Show alteration history of a file with the name of the author
git blame [filename] -l #Show alteration history of a file with the name of the author && SHA
Logs
git log #View changes
git log -p #List of commits showing commit messages and changes
git log -S 'something' #List of commits with the particular expression you are looking for
git log --author 'Author Name' #List of commits by author
git log --oneline #Show a list of commits in a repository in a more summarised way
git log --since=yesterday #Show a list of commits in a repository since yesterda
git log --grep "term" --author "name" #Shows log by author and searching for specific term inside the commit message
git shortlog -s --author 'Author Name' #Number of commits by author
git shortlog -s -n #List of authors and commits to a repository sorted alphabetically
git shortlog -n --author 'Author Name' #This also shows the total number of commits by the author
Stashing files
git stash #Stash local changes
git stash save "this is your custom message" #Stash local changes with a custom message
git stash apply #Re-apply the changes you saved in your latest stash
git stash pop #Apply the stash and then immediately drop it from your stack
git stash list #List all stashes
Tags
git tag # show list of tag
git tag -a v1.0.0 -m "message" # give a tag to this commit
git push --tags # push the tags to origin
git fetch --tags # update local with remote tags
git tag -d <tag-name> # delete a tag locally
git push origin :refs/tags/<tag-name> # delete a tag from remote
git tag --contains <commit> # list of tags contain a commit
git describe --exact-match <commit> # check if the commit contains tag(s)
git checkout <tag-name>
config
git config --global user.name <your-name> # saved in (~/.gitconfig) file
git config --global user.email <your-email>
git config --local user.name <your-name> # saved in <repo>/.git/config
git config --local user.email <your-email>
git config --list # See full configuration of git
git config --list --show-origin # See full configuration with config file path
git config --global --unset credential.helper # unset git credentials
sudo find / -type f -name .git-credentials # `~/.git-credentials` or `~/.config/git/credentials` save the credentials
sudo find / -type f -name credentials
# for `windows` credential.helper = manager
git config --global credential.helper manager
git credential-manager delete <url> # delete credential-manager