Git Commands - ankit-jha/commands GitHub Wiki

Config commands

Global Config commands

git config --global user.name "johndoe"

git config --global user.email [email protected]

git config --global diff.tool vimdiff #Sets vimdiff as the default diff tool for all git projects

git config --global difftool.prompt false #Disables the prompt when opening vimdiff for all git projects

git config --global alias.vimdiff difftool #Sets git vimdiff as the alias for difftool

git config --global difftool.trustExitCode true #Allows to use :cquit command to exit the difftool

git config --global -e #Edit global configuration

git config --global -l #List global configuration

Local Config commands

git config user.name "ankit-jha" #Sets it for your local project folder.

git config user.email [email protected] #Sets it for your local project folder.

git config -e #Edit local configuration

git config -l #List local configuration

git config merge.tool vimdiff #Sets git as the default merge tool for the current project

git config mergetool.prompt false #Disables the prompt to open the vimdiff for the current project

git config mergetool.trustExitCode true #Allows to use :cquit command to exit the mergetool

git config merge.conflictstyle diff3 #Display a common ancestor while merging for the current project

Tag commands

git tag -a v0.1 -m "demo" #Tags the current branch with version number 0.1 and description "demo"

git push origin v0.1 #Push the tag to remote

git tag --contains e459bce #Show all the tags containing the commit e459bce

git show tags/8.3.0:path/to/file #Show the file in the tag 8.3.0

git fetch upstream --tags # Fetch all tags from upstream remote

git fetch upstream dev # Fetch only dev branch from upstream remote

git fetch --all --tags # Fetch all tags from all remotes

Branch commands

git branch #Shows all the local branches

git branch -r #Shows all the remote branches

git branch -a #Shows all the branches

git branch -m newname #Rename the branch

git branch -d dev1 #Deletes dev1 a fully merged branch

git branch -D dev1 #Forcefully deletes dev1 branch

git branch -u origin/dev #Set (or change) a tracking relationship for your current HEAD branch

git branch -r --list origin* #List all the remote branches that have pattern origin*

git branch --sort=-committerdate # Get a list of Git branches, ordered by most recent commit

git branch --sort=committerdate # Get a list of Git branches, ordered by oldest commit

git push origin --delete dev1 #Deletes remote origin/dev1 branch

git fetch --all --prune # Fetches all remotes and also Prunes all the obsolete remote tracking branches on local

git branch --set-upstream-to=origin/branch local_branch #Sets up tracking for local_branch with remote origin/branch

Checkout commands

git checkout dev # Switches to dev branch

git checkout -b dev_new # Creates a new branch and switches to it

git checkout --track origin/dev # Check out a remote tracking branch dev on origin(especially if there are two or more remotes with the same branch name e.g. upstream and origin)

git checkout e459bce path/to/file #Check out specific file from a commit

git checkout -b dev origin/dev #Check out branches that exist on multiple remotes

git checkout -- path/to/file(s) #Checkouts only given file(s).

git checkout --theirs path/to/file #Resolving git conflict in favor of their changes after running git merge

git checkout --ours path/to/file #Resolving git conflict in favor of our changes after running git merge

git checkout -p dev path/to/file #Merge specific file from dev branch. This will create a patch and apply them as per user permission in hunks. Article

Rebase commands

Squashing Commits

git rebase -i HEAD~4 #Squashes last 4 commits together. Choose option squash in the interactive editor and after that customize the commit message

Patch commands

Create Patch

git diff --patch > some-modifications.patch #Generates a patch file

git diff --patch --staged > some-modifications.patch #Generates a patch file from staged files

git diff --patch file1.txt src/ > some-modifications.patch #Generates a patch file for file1.txt and changed files under src/ folder git show --patch HEAD^ > some-modifications.patch #Generates a patch for just the HEAD commit

git show --patch HEAD~3 > some-modifications.patch #Generates a patch for the previous 3 commits from HEAD

Apply Patch

git apply -- some-modifications.patch #Applies the patch as unstaged changes.

pbpaste | git apply - #Applies the patch from clipboard

curl -L https://github.com/functions/pull/161.patch | git apply #Applies a pull request #161 patch as unstaged changes

git am some-modifications.patch #Applies the patch as committed changes.

Revert Patch

git apply -R some-modifications.patch #Reverts back unstaged patch changes

git show | git apply -R #Reverse apply a patch from a commit revision.

Remote commands

git remote rename origin new_origin

git remote add upstream url

git remote set-url origin new_url #In case of migrated repos one can set the new remote with this command

git remote update #Updates all the remotes

git remote update --prune #Updates all the remotes and prunes the deleted remotes

git remote show upstream #Gives information about the remote and it's branches

Reset commands

git reset --hard origin/master #This resets your local master in sync with remote branch origin/master

git reset filename #This unstages the file added to index

Log commands

git log --follow filename #Shows commits only for that specific file

git log -5 #Shows last 5 commits

git log --oneline #One line info about each commit

git log --author="developer name" #Shows commits from a particular developer

git log --since="2 days ago" #Shows commits from last two days

git log --grep="JRA-224:" #Searches the commit message

git log --pretty=short -u -L ,:<path_to_filename> #Searches the commit message

git log --graph #Visualize commit history in a graph

Clean commands

git clean -f #Removes untracked files from the directory

git clean -n #Shows list of untracked files that will be removed

git clean -d #Removes untracked directory

Cherry Pick commands

git cherry-pick dev #Apply the change introduced by the last commit of the dev branch and create a new commit with this change.

git cherry-pick -n dev~1 #Apply the change introduced by the second last commit of the dev branch and index them but don't commit.

git cherry-pick ebe6942..905e279 dev #Apply commits from ebe6942 to 905e279 where ebe6942 is not included.

git cherry-pick ebe6942^ dev #Apply commits from ebe6942 to last commit of the dev branch.

git cherry-pick -n ebe6942 #Add commits from ebe6942 to stage of the dev branch.

Stash commands

git stash #Saves unfinished changes of working directory both modified tracked files and staged changes on a stack but not untracked files.

git stash -u #Saves unfinished changes of working directory including untracked files.

git stash save workStash #Saves the stash with name workStash.

git stash apply #Apply the most recent stashed changes

git stash apply stash@{1} #Apply the second most recent stashed changes

git stash apply stash^{/local_project_changes} #Apply the stashed changes with name

git stash pop #Apply the most recent stashed changes and also drop the stash

git stash show -p stash{0} #Show the patch for stash{0}

git stash -- file.txt #Stashes only the given file.txt

Vim-Fugitive Commit Workflow

Article

Other Frequently Used Git Commands

git diff HEAD~1 com/src/file.text #Diff between current and previous version of file.txt

git diff --name-only upstream/dev path/to/folder #Show name of the diff files for the given folder

git diff origin/dev -- file.txt #Diff between file.txt in the checked out branch vs file.txt present at remote origin/dev

git diff origin/dev origin/test -- file.txt #Diff between file.txt present at remote origin/dev vs file.txt present at remote origin/test

git difftool --tool=vimdiff com/src/file.text #Side by Side Diff between current and previous version of file.txt

git commit --amend #Amend the most recent commit message

git commit --amend --author 'Ankit Jha <[email protected]>' --no-edit #Amend only the author of the most recent commit

git commit --amend --reset-author --no-edit #Reset the author on the last commit. It will pick up any recent author(user.email) changes

git commit --amend HEAD~3 #Amend commit messages for last 3 commits

Understanding commit amends

git pull remote branch #Fetches and merge commits from that remote branch.

git pull --rebase remote branch #Fetches and rebase commits from that remote branch.

git pull -s recursive -X theirs #Or, simply, for the default repository:git pull -X theirs #This resolves git merge conflicts in favor of their(remoterepo) changes by overriding them during a pull

git clone -b develop [email protected]:ankit-jha/commands.git #Clone a specific branch

git rm file.txt #Delete file from the repository as well as file system

git add . #Adds files to index only from current and children folders

git add -u #Adds modified/deleted files to the index

git add --patch #Displays changes to be indexed(staged) in form chunks. Makes indexing process easy.

git ls-files #Displays all the files currently part of the git repo.

git ls-files -m #Displays all the modified(and deleted) files

git grep "copyright" #Search for term "copyright" in all the git files

git submodule update --remote #Update a git submodule from it's remote

git push -u origin dev_new #The first time that you push dev_new branch it creates a new remote branch and also sets it to track remote branch 'dev_new' from 'origin'.

git --no-ff #The --no-ff flag causes the merge to always create a new commit object, even if the merge could be performed with a fast-forward. This avoids losing information about the historical existence of a feature branch and groups together all commits that together added the feature

git show e459bce #Show changes done for particular commit

git show e459bce..68ceb70 #Show all changes done between two commits

git show e459bce..68ceb70 path/to/file #Show all changes done between two commits for a particular file

git show e459bce --name-only #Show changed files for particular commit

git merge dev #Merges the dev into current branch

git merge --abort #Abort the merge after 'git merge'

git revert e459bce #Revert back particular commit and also create a new commit

git revert --no-commit e459bce #Revert back particular commit but doesn't create a new commit

git remote prune origin #Prune local tracking branches that do not exist on remote anymore. You can add flag --dry-run to verify the results before actual run.

git diff --color --name-only --diff-filter=M dev #Shows names of only changed files between local and dev branch

--diff-filter=[ACDMRTUXB*]

Select only files that are

    A Added
    C Copied
    D Deleted
    M Modified
    R Renamed
    T have their type (mode) changed
    U Unmerged
    X Unknown
    B have had their pairing Broken
    * All-or-none

Any combination of the filter characters may be used.

When * (All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.

.git/info/exclude is a place where you can add files or folders to if you don't want to create a .gitignore file to share with others, you can create rules that are not committed with the repository. You can use this technique for locally-generated files that you don't expect other users to generate, such as files created by your editor.

https://help.github.com/en/articles/ignoring-files#explicit-repository-excludes

⚠️ **GitHub.com Fallback** ⚠️