Git Commands Cheat Sheet
Git Cheat Sheet
Here is a list of the Git commands mentioned throughout the eBook
- Git Configuration
Before you initialize a new git repository or start making commits, you should set up your git identity.
To change the name that is associated with your commits, you can use the git config
command:
The same would go for changing your email address associated with your commits as well:
That way, once you have the above configured when you make a commit and then check the git log, you will be able to see that the commit is associated with the details that you’ve configured above.
In my case the output looks like this:
Initializing a project
To initialize a new local git project, open your git or bash terminal, cd
to the directory that you would like your project to be stored at, and then run:
If you already have an existing project in GitHub, for example, you can clone it by using the git clone command:
Current status
To check the current status of your local git repository, you need to use the following command:
This is probably one of the most used commands as you would need to check the status of your local repository quite often to be able to tell what files have been changed, staged, or deleted.
Add a file to the staging area
Let’s say that you have a static HTML project, and you have already initialized your git repository.
After that, at a later stage, you decide to add a new HTML file called about-me.html
, then you’ve added some HTML code in there already. To add your new file so that it is also tracked in git, you first need to use the git add
command:
This will stage your new file, which essentially means that the next time you make a commit, the change will be part of the commit.
To check that, you can again run the git status
command:
You will see the following output:
Removing files
To remove a file from your git project, use the following command:
Then after that, if you run git status
again, you will see that the some_file.txt
file has been deleted:
Discard changes for a file
In case that you’ve made a mistake and you want to discard the changes for a specific file and reset the content of that file as it was in the latest commit, you need to use the command below:
This is a convenient command as you can quickly revert a file to its original content.
Commit to local
Once you’ve made your changes and you’ve staged them with the git add
command, you need to commit your changes.
To do so, you have to use the git commit
command:
This will open a text editor where you could type your commit message.
Instead, you could use the -m
flag to specify the commit message directly in your command:
List branches
To list all of the available local branches, just run the following command:
You would get a list of both local and remote branches, the output would look like this:
The remotes
keyword indicates that those branches are remote branches.
Fetch changes from remote and merge the current branch with upstream
If you are working together with a team of developers working on the same project, more often than not, you would need to fetch the changes that your colleagues have made to have them locally on your PC.
To do that, all you need to do is to use the git pull
command:
Note that this will also merge the new changes to the current branch that you are checked into.
Create a new branch
To create a new branch, all you need to do is use the git branch
command:
Instead of the above, I prefer using the following command as it creates a new branch and also switches you to the newly created branch:
If the branch_name
already exists, you would get a warning that the branch name exists and you would not be checked out to it,
Push local changes to remote
Then finally, once you’ve made all of your changes, you’ve staged them with the git add .
command, and then you committed the changes with the git commit
command, you have to push those changes to the remote git repository.
To do so, just use the git push
command:
Delete a branch
Switch to a new branch
As mentioned above, if you add the -b
flag, it would create the branch if it does not exist.
Conclusion
Knowing the above commands will let you manage your project like a pro!
If you are interested in improving your command line skills in general, I strongly recommend this Linux Command-line basics course here!