Accidentally pushed your changes to the wrong branch? Yeah, it happens to me as well. And instead of searching for the solution online every time it occurs, I started writing down the git commands and workflows I use to perform common tasks.
This article will cover git commands for the usual scenarios you encounter when working on a git project, from the basics of how to clone a repository and create a separate branch to handling common issues such as committing to the wrong branch or copying a single file from a different branch into the current one.
Cloning the repository
The first thing you want to do when starting to work on a git repository is to clone it to your computer. You need to position yourself to the folder in which you’d like to clone your repository and run the following command:
git clone <url>
Where <url> represents the URL of the git repository you want to clone.
Creating a branch
Unless you’re working alone on a personal project (and sometimes even then), it’s not a good idea to make changes directly into the master branch. In fact, many projects with multiple contributors won’t allow you to do this. So whenever you’re working on new functionality, you’ll typically create a new branch from the master branch. Once your work is done (tested and reviewed), you can merge it into the master branch and make it a part of the project.
If you’re working on a project that has other contributors, you should make sure you have the latest version of the master branch by pulling the existing changes from the remote repository using the following command:
This command might complain that you haven’t set up an upstream branch for your local master branch when you run it for the first time. You can fix this by running:
git branch --set-upstream-to=origin/master master
(Git will probably suggest this command for you when you try running git pull.)
Once you’ve successfully pulled the latest changes, you can create a new branch by running:
git checkout -b <branch_name>
And replacing <branch_name> with the desired name for your branch.
Committing and pushing your changes
You created your changes and now you’d like to push them to the remote git repository. You first need to create a commit to your local repository and then push your local commits to the remote.
Firstly, add all the files you changed to your local commit. You can do that by using the add command. Typically, you’ll want to add all your changes to the commit by running:
git add *
But you can also add individual files or groups of files by providing a specific path to the add command:
git add <path>
Note that the <path> is a relative path inside your git repository.
You can check which files have been changed and which of them have been added by running:
If you added some files by mistake, you can always revert it by running:
git restore <path>
Once you added the right files, you can commit your changes by running:
git commit -m “<Message describing your commit>”
After committing your changes, you can push them to the remote repository by running:
(When you run this command for the first time in a new branch, git will ask you to set up an upstream branch for this branch on the remote repository. You can simply run the command git suggests you in the prompt to do this.)
Switching to another branch
If you want to switch to another existing branch, you can do it by running
git checkout <branch_name>
Merging the master branch into a local branch
Someone made changes into the master branch that you would like to use in your local branch? You can accomplish this by merging all the changes from the master branch into your local branch.
Beware that git will merge the changes in your local master branch so you first want to make sure to pull the changes from the remote master branch into your local one. You’ll need to switch to the master branch and run the pull command:
git checkout master
After that, you can switch back to your local branch and merge the master branch into it:
git checkout <branch_name>
git merge master
Merging another branch into your local branch
You can merge any other branch in the same way as merging the master branch by replacing the name “master” with the name of that branch.
However, if you want to merge a branch someone else created on the remote repository, you will first have to download it to your local repository by running:
When you merge two branches, it could happen that both branches changed the same lines of code, but in a different way. Which change should be kept? Unfortunately, git can’t read minds yet so it will create a merge conflict when this happens. Upon running the merge command, git will list all files in which merge conflicts occur. You will have to manually edit the files and then commit your changes using the add and commit commands like you would for a normal commit.
Accidentally committing changes into the wrong branch
What if you accidentally committed changes into the wrong branch? You can use the reset command:
git reset --soft HEAD~1
This command will undo the latest commit, but keep all of your changes. So you can simply create a new branch or switch to an existing one and then add and commit your changes there.
Accidentally committed more than one wrong commit? You can run the same command with any number of commits:
git reset --soft HEAD~<number_of_commits>
You committed come changes, but now they’re no longer desired. You can delete them by running:
git reset --hard HEAD~<number_of_commits>
This will remove the commits and it won’t keep the changes.
Copying a specific file from another branch
There are many reasons why you might want to do this. Maybe you committed changes to several files, but now you only want to revert changes from one of them. Or maybe you don’t want to merge an entire branch, but you need changes made in one file of that branch.
You can copy the file (or a group of files) using the checkout command. (Note that you should run the command from the target branch to which you want to copy the changes.)
git checkout <branch_name> -- <path>
Where <branch_name> is the name of the source branch from which you want to copy the changes and <path> is a path to file (or files) that you want to copy from the branch.
If you want to restore files (revert your changes), you can simply checkout the files from the master branch.
I hope you will find these useful in your everyday git struggles. Thank you for reading!