Git Commands & Use Cases
Here are some of the git commands that I most often use are presented here with use cases. This is not an exhaustive list of git commands.
- Create a local repo and push to remote repo
- Create a new repo
git init
- Initializes a new local git repo with local branch name asmain
- Push local repo to remote
- Prerequisite: Create a remote repo in github/gitlab/bitbucket
- Connect/Link local repo to remote repo
git remote add origin <github-repo-link>
- Example:
git remote add origin git@github.com:<user-name>/<repo-name>.git
git pull origin main
git push -u origin main
- Create a new repo
- Create a local feature branch, remote feature branch, and link local branch to remote branch
- Create a local branch
git checkout -b <local-branch-name>
- When you do
git push
- Git complains:
The current branch <local-branch-name> has no upstream branch.
- Git complains:
- Time to create a remote branch to do any push
git push -u origin <remote-branch-name> // -u shorthand for --set-upstream
- Example:
git push -u origin dev/feature
- Example:
- The above command will create a remote branch and links the local-branch with remote branch, thereby enabling us to do git push easily without
-u
argument
- Create a local branch
- Create a local feature branch, link to remote branch in one go
git checkout -b <localbranch> <remote-branch>
// This will automatically add the –track option- Example:
git checkout -b dev/feature1 origin/dev/feature1
- Discard/Revert all your local commits and reset local branch to last stable commit of remote branch
- Prerequisite: Ensure shell/prompt is in local branch
git reset --hard origin/<remote-branch-name>
- Revert to a specific commit: Erase history of some commits
git reset --hard <commitId>
git push -f // -f shorthand for --force -> force updates
- Move all changes from staging to unstaged area
git reset
- Move a specific file from staging to unstaged area
git restore --staged <Path of the file you wanna restore>
- Replace all changes in unstaged area with latest from upstream
git checkout .
- Replace a specific file in unstaged area back with latest from upstream
git checkout <abs-path-filename>
- Remove the untracked file/directories
git clean -f // cleans all untracked files
git clean -f -d // cleans all untracked directories
- Show the files changes for a given commit
git show --pretty="" <commit-hash> --name-only
- Delete a local branch
git branch -D <local-branch-name>
- D indicates delete a local branch even if it is not merged
- Delete a remote branch
git push --delete origin master
- This will delete the remote branch
- Merge/Sync local branch (A) with remote branch (A)
- Ensure prompt/shell in local branch
git checkout branch-A
git pull origin/branch-A
// Syncs local branch-A with remote origin/branch-A- Or simply
git pull
// This requires local branch to be linked with remote branch
- Merge/Sync local branch(A) with remote branch (B) Example: B can be master/dev branch
- Ensure prompt/shell in local branch
git pull origin <remote-branchname>
- Example:
git checkout feature1
git pull origin development
// Syncs local branch feature1 with remote development branch
- Switch between branches, if you are in Branch (A) and want to switch to another Branch (B)
git checkout <branch>
// This will simply switch your local branch- Or
git switch <branch>
// Starting from git2.23
- Update commit message - Works only if it’s not already pushed to upstream
git commit --ammend
- Stash commands
- To view the stashed file changes on console
git stash show -p 18
// here 18 is your stash number
git stash -u(includes untracked files as well) -m <named your stash>
- Stashes all your staged and unstaged commits
- Example:
git stash -u -m "srk-local-automation"
git stash list
- Lists all your stashes
git stash show
- You can view the summary of a stash
- Lists all the files present in this stash
git stash apply <number given to your stash>
- Doesn’t remove the last saved stash, helps for later reuse
- Example:
git stash apply 0 or git stash apply stash@{0}
- Export the stash:
git stash show -p --binary > srk-local-changes.patch
- This will create an exportable file
srk-local-changes.patch
which you can apply on other repos
- This will create an exportable file
- Import the stash:
git apply srk-local-changes-patch
- This will import the patch file
srk-local-changes-patch
, meaning it is added to current stash list
- This will import the patch file
git stash pop <name of your stash>
- Removes the last saved stash from top of stash and applies
- To view the stashed file changes on console
- Tag commands
- Create an annotated tag
git tag -a v0.1
- List all the tags
git tag
- Get the details of a specific git tag
git show v0.1
- Note: git push doesn’t automatically push your tags into the remote server. So you have to push the tag explicitly using the below command
git push origin <your-tag>
- Example:
git push origin v0.1
- Create an annotated tag
References: