I like to maintain a cheatsheet of git commands which I frequently use. The repo contains the most used commands and a small info about them which will improve your productivity and save precious hours by randomly going through stackoverflow questions.
Feel free to reach out to me! 😊
Instagram || Twitter || LinkedIn || Blog
This repository is contribution friendly😃. If you would like to improve or add some useful commands, your contribution is welcome!
- Awesome place to learn git
- Basic Terminologies
- Baisc Flow
- Git Branch
- Git Stash
- Miscellaneous
- Delete Branch
- Open Source Contribution
- GitHub Learning Lab
- Git Started with GitHub
- Version Control with Git
- Atlassian Git Tutorial
- Lean Git Branching
- git: Git is a distributed version-control system for tracking changes in source code during software development.
- git clone: To download an existing git repository on your local machine.
- git fetch: It only downloads the new data from remote repository and does not integrate that data into working files.
- git pull: It downloads the new data as well as merges it into the existing files.
-
Adding git to a folder(Execute inside the project folder)
git init
-
Adding and commiting project files
git add .
git commit -m "commit message"
-
Create a README.md file
-
Create a remote repository on Github/Gitlab/Bitbucket(Do not create README.md file here)
-
Linking your local repository to your remote repository
git remote add origin $URL_TO_YOUR_REMOTE_REPOSITORY
-
Pushing local files to remote repository
git push origin master
-
To fetch any changes made on remote repository to local repository
git pull origin master
-
Get list of branches on local
git branch -a
-
Get list of branches on remote
git branch -r
-
Create a branch
git checkout -b $BRANCH_NAME
git push origin $BRANCH_NAME
-
Get list of only local branches
git branch -vv | cut -c 3- | awk '$3 !~/\[/ { print $1 }'
-
Display commit logs
git log --oneline --graph --decorate --all
-
Rename a local branch
i. Renaming the current branch:
git branch -m $NEW_BRANCH_NAME
ii. Renaming some other branch:
git branch -m $OLD_BRANCH_NAME $NEW_BRANCH_NAME
-
Rename a remote branch
i. First complete the above two steps.
ii. Delete the old-name branch and push the new-name local branch:
git push origin :$OLD_BRANCH_NAME $NEW_BRANCH_NAME
iii. Reset the upstream branch for new-name local branch
git push origin -u $NEW_BRANCH_NAME
-
Delete a local branch
git branch -d $BRANCH_NAME
git branch -D $BRANCH_NAME
-d stands for --delete, which will delete the local branch, only if you have already pushed and merged it with your remote branches.
-D stands for --delete --force, which deletes the branch regardless of its push and merge status.
-
Saving uncommited changes(both staged and unstaged) and making the working branch clean
git stash
-
Saving with a message
git stash save $MESSAGE
-
Checkout all the stash in your project
git stash list
-
Pasting the stash changes and remove it from list
git stash pop
-
Apply ths stash changes and also keep them in the list
git stash apply
-
Creating a branch from stash
git stash branch $BRANCH_NAME
-
Delete all your stash from the project
git stash clear
-
Remove a particular stash from the list
git stash drop $STASH_ID
-
Discarding unstaged changes
git checkout -- .
-
Checkout conflict files
git diff --name-status --diff-filter=U
-
Delete Local Branch
git branch -d $BRANCH_NAME
-
Delete Global Branch
git push origin --delete $BRANCH_NAME
-
Fork the desired repository and clone that fork repo to your local machine.
-
Fetching changes from main repository and merging them into your local copy.
i.git remote add upstream $URL_TO_YOUR_ORIGIN_REPOSITORY
ii.git fetch upstream
iii.git merge upstream/master
iv. Now push these change to your remote repository
git push