Practical guide to everyday Git commands
Show your support by giving a ⭐ to this repo
git clone URL
The repo URL can be found inside the repo.
Github repo link example
Bitbucket repo link example
By default the main/master branch is active. Ask which branch has the relevant code
cd repo<-name>
git switch <branch-name>
Note: The checkout command can also be used to switch the branch. git checkout <branch-name>
git pull
First stage all the files and then commit your changes. You can create multiple commits.
git add *
git commit -m 'Your commit message'
You can add files by mentioning the file/files with relative or full path. You can add files one by one or multiple files at a time using the commands
git add <file-path>
git add <file1-path> <file2-path>
Similarly, to unstage a file use the command git reset <file>
git reset --hard
git checkout HEAD -- <file-path>
You can undo the commit by resetting the HEAD. If you just want to undo the commit but let the changes be present then use the soft
attribute else if you do want the commit along with the changes then use the hard
attribute
git reset --soft HEAD~1
(undo with changes preserved)git reset --hard HEAD~1
(undo with changes removed)The command git pull
may not work if the changes are done by someone else to the same files which you have also modified.
git stash save <name of the change> -u
git pull
git stash apply <n>
where n is the stash number. To get the list of stashes git stash list
Note: You can stash multiple changes and bring them back as and when you like to
If there are changes in the code on the region of the stashed code, it is expected to get conflicts. You will have to manually resolve all the conflicts. (Do it carefully)
Below is the view of the VSCode, which helps in easily resolving the conflicts.
You can create a separate branch out of the current branch and commit it. This works both if you have already made changes or are yet to start making changes.
git switch -c <my-branch-name>
git add *
git commit -m "<some commit message>"
To switch between the branches use the command git checkout <original-branch-name>
Alternatively, checkout command can also be used to create the branch. git checkout -b <my-branch-name>
Note: my-branch-name
is your local branch and not available for anyone else unless you push it
Yes. You can update the commit by amending your changes.
git add <file-name> <file-name> ...
git commit --amend --no-edit
To update with new commit message git commit --amend -m 'My new commit message'
(This command can also be used to simply update the previous commit message without any code modifications)
--no-edit
is used to avoid the prompt to edit the commit message. If you want to modify the commit message as well during the update of the commit, then do not include -m
along with your commit message
Note: The amend
updates the previous commit without creating a new one on top of the previous. (in reality, Git discards the previous commit and replaces it with a new commit)
You can follow the same steps as given in the previous question. Once done you will push the code and raise a PR. It's that simple. Here we assume you are on the 'develop' branch and raising PR to the 'main' branch.
git switch -c <my-branch-name>
git add *
git commit -m "<some commit message>"
git push
(as the branch is not present on the remote, it will show the command to use)git push --set-upstream origin <my-branch-name>
The URL to raise the PR will be automatically available as shown above. Use the link and open it in the browser.
Now select the base branch to which you want to raise a PR and click on 'Create a PR'
To delete a branch locally, check out a different branch than the one you want to delete. Here we will delete the branch named 'develop'
git checkout <other-branch>
git branch -d <branch-name>
To delete the branch from remote as well
git push -d <remote> <branch-name>
Note: If -d
does not allow to delete a branch, use the -D
. Example: git branch -D <branch-name>
To rename a branch, checkout to the branch and rename it.
git checkout <your-branch-name>
git branch -m <new-branch-name>
git push <remote> :<your-branch> <new-branch-name>
git push <remote> -u <new-branch-name>
To move the changes from your local machine to remote (called origin), follow the below steps in your terminal
git add *
git commit -m "<some commit message>"
git push
Yes. You can update the commit even after it is pushed. Everything will follow as mentioned in the previous question, but you will have to force push.
git add <file-name> <file-name> ...
git commit --amend --no-edit
or git commit --amend
git push -f
or git push --force
Note: You need to be very careful while pushing forcefully, as it may eliminate other commits if someone has done in between. Make sure you are working on the branch and no one else is simultaneously working on the same or branching out from the branch at your commit.
The rejection could be because the remote branch might be ahead of the local branch. Different techniques can be used here to achieve sync.
git pull
git push
If there are conflicts, then resolve them manually to proceed ahead as shown below.
If there are code changes on the same region from multiple commits, conflicts will occur. You need to resolve all the conflicts and proceed.
git add <file-path>
git merge --continue
git push
Note: If something goes wrong, in any of the above steps, then there is nothing to panic about. Just run git merge --abort
and redo the steps.
Yes. You can pull the changes without a merge. This is called Rebase. I know you have heard it a lot. It is very simple though.
git pull --rebase
git push
If you get conflicts, then solve all the conflicts. Then
git rebase --continue
git push
Note: If something goes wrong, in any of the above steps, then there is nothing to panic about. Just run git rebase --abort
and redo the steps.
PR will show conflicts if the new changes added to the source branch are conflicting with your changes or your branch is lagging.
There are 2 main approaches to solve this.
Follow any one of the approaches. Don't try both of them.
Assuming that your branch is develop
and the source branch is main
git checkout main
git pull
git checkout develop
git merge main
git add <files>
git merge --continue
git push
git checkout main
git pull
git checkout develop
git rebase main
git add <files>
git rebase --continue
after resolving the conflictsgit push -f
Note: You may have to run git rebase --continue
multiple times if there are multiple conflicts on your multiple commits.
Converting multiple commits into one is known as Squashing. We will achieve this by rebasing with the help of the interactive feature of the editor (VSCode). This will be both easier and simple.
git rebase -i HEAD~<n>
(where n refers to the number of commits to squash)Note: If you had already pushed the commits, then you will have to use the command git push -f
to reflect squash on the remote branch as well.
You can use the technique of undoing to achieve this easily.
git reset --soft HEAD~<n>
git commit -m 'Commit message'
Note: If you had already pushed the commits, then you will have to use the command git push -f
to reflect squash on the remote branch as well.
To copy the changes of a commit from one branch to another, you can use cherry pick. First obtain the commit id of the commit, which you want to copy.
git checkout <destination-branch>
git cherry-pick <commit-id>
If you get any conflicts, resolve the conflicts first. Once all the conflicts are resolved, they continue the cherry-pick.
git add <file-paths>
git cherry-pick --continue
Note: If something goes wrong in between, reset the process by using the command git cherry-pick --abort
(You can start the process again)
To copy a range of commits from one branch to another, you can note down the older commit id from history and the newer commit id.
git checkout <destination-branch>
git cherry-pick <old-commit-id>..<new-commit-id>
Revert creates a reverse commit where the changes made will reverse and is created as a new commit. To revert a particular commit first, obtain its commit id.
git revert <commit-id>
git push
When resetting a branch to a different branch, the codebase on your branch will become the same as the other branch. To achieve this, you can use the 'reset' command.
git checkout <your-branch-bame>
git reset --hard <source-branch>
In this case, 'your-branch-name' will match the codebase of 'source-branch'
Revert will reverse the changes creating a new commit. If you want to remove the previous commit, then you can undo and force push the branch.
git reset --hard HEAD~<n>
(n is the number of commits to undo)git push -f
Note: Force push is needed as the history of the branch is changing in this. If the commit is not pushed then it is not needed.
This repository is MIT licensed. Read more