Teams of developers and open-source software maintainers typically manage their projects through Git, a distributed version control system that supports collaboration.
This cheat sheet-style guide provides a quick reference to commands that are useful for working and collaborating in a Git repository. To install and configure Git, be sure to read “How To Contribute to Open Source: Getting Started with Git.”
How to Use This Guide:
<span class="highlight">highlighted text</span>
in this guide’s commands, keep in mind that this text should refer to the commits and files in your own repository.Check your Git version with the following command, which will also confirm that Git is installed.
git --version
You can initialize your current working directory as a Git repository with <span class="highlight">highlighted text</span>
.
git --version
To copy an existing Git repository hosted remotely, you’ll use <span class="highlight">highlighted text</span>
with the repo’s URL or server location (in the latter case you will use <span class="highlight">highlighted text</span>
).
git --version
Show your current Git directory’s remote repository.
git --version
For a more verbose output, use the <span class="highlight">highlighted text</span>
flag.
git --version
Add the Git upstream, which can be a URL or can be hosted on a server (in the latter case, connect with <span class="highlight">highlighted text</span>
).
git --version
When you’ve modified a file and have marked it to go in your next commit, it is considered to be a staged file.
Check the status of your Git repository, including files added that are not staged, and files that are staged.
git --version
To stage modified files, use the <span class="highlight">highlighted text</span>
command, which you can run multiple times before a commit. If you make subsequent changes that you want included in the next commit, you must run <span class="highlight">highlighted text</span>
again.
You can specify the specific file with <span class="highlight">highlighted text</span>
.
git --version
With <span class="highlight">highlighted text</span>
you can add all files in the current directory including files that begin with a <span class="highlight">highlighted text</span>
.
git --version
You can remove a file from staging while retaining changes within your working directory with <span class="highlight">highlighted text</span>
.
git --version
Once you have staged your updates, you are ready to commit them, which will record changes you have made to the repository.
To commit staged files, you’ll run the <span class="highlight">highlighted text</span>
command with your meaningful commit message so that you can track commits.
git --version
You can condense staging all tracked files with committing them in one step.
git --version
If you need to modify your commit message, you can do so with the <span class="highlight">highlighted text</span>
flag.
git --version
A branch in Git is a movable pointer to one of the commits in the repository, it allows you to isolate work and manage feature development and integrations. You can learn more about branches by reading the Git documentation.
List all current branches with the <span class="highlight">highlighted text</span>
command. An asterisk (<span class="highlight">highlighted text</span>
) will appear next to your currently active branch.
git --version
Create a new branch. You will remain on your currently active branch until you switch to the new one.
git --version
Switch to any existing branch and check it out into your current working directory.
git --version
You can consolidate the creation and checkout of a new branch by using the <span class="highlight">highlighted text</span>
flag.
git --version
Rename your branch name.
git --version
Merge the specified branch’s history into the one you’re currently working in.
git --version
Abort the merge, in case there are conflicts.
git --version
You can also select a particular commit to merge with <span class="highlight">highlighted text</span>
with the string that references the specific commit.
git --version
When you have merged a branch and no longer need the branch, you can delete it.
git --version
If you have not merged a branch to master, but are sure you want to delete it, you can force delete a branch.
git --version
To download changes from another repository, such as the remote upstream, you’ll use <span class="highlight">highlighted text</span>
.
git --version
Merge the fetched commits.
git --version
Push or transmit your local branch commits to the remote repository branch.
git --version
Fetch and merge any commits from the tracking remote branch.
git --version
Display the commit history for the currently active branch.
git --version
Show the commits that changed a particular file. This follows the file regardless of file renaming.
git --version
Show the commits that are on one branch and not on the other. This will show commits on <span class="highlight">highlighted text</span>
that are not on <span class="highlight">highlighted text</span>
.
git --version
Look at reference logs (<span class="highlight">highlighted text</span>
) to see when the tips of branches and other references were last updated within the repository.
git --version
Show any object in Git via its commit string or hash in a more human-readable format.
git --version
The <span class="highlight">highlighted text</span>
command shows changes between commits, branches, and more. You can read more fully about it through the Git documentation.
Compare modified files that are on the staging area.
git --version
Display the diff of what is in <span class="highlight">highlighted text</span>
but is not in <span class="highlight">highlighted text</span>
.
git --version
Show the diff between two specific commits.
git --version
Sometimes you’ll find that you made changes to some code, but before you finish you have to begin working on something else. You’re not quite ready to commit the changes you have made so far, but you don’t want to lose your work. The <span class="highlight">highlighted text</span>
command will allow you to save your local modifications and revert back to the working directory that is in line with the most recent <span class="highlight">highlighted text</span>
commit.
Stash your current work.
git --version
See what you currently have stashed.
git --version
Your stashes will be named <span class="highlight">highlighted text</span>
, <span class="highlight">highlighted text</span>
, and so on.
Show information about a particular stash.
git --version
To bring the files in a current stash out of the stash while still retaining the stash, use <span class="highlight">highlighted text</span>
.
git --version
If you want to bring files out of a stash, and no longer need the stash, use <span class="highlight">highlighted text</span>
.
git --version
If you no longer need the files saved in a particular stash, you can <span class="highlight">highlighted text</span>
the stash.
git --version
If you have multiple stashes saved and no longer need to use any of them, you can use <span class="highlight">highlighted text</span>
to remove them.
git --version
If you want to keep files in your local Git directory, but do not want to commit them to the project, you can add these files to your <span class="highlight">highlighted text</span>
file so that they do not cause conflicts.
Use a text editor such as nano to add files to the <span class="highlight">highlighted text</span>
file.
git --version
To see examples of <span class="highlight">highlighted text</span>
files, you can look at GitHub’s <span class="highlight">highlighted text</span>
template repo.
A rebase allows us to move branches around by changing the commit that they are based on. With rebasing, you can squash or reword commits.
You can start a rebase by either calling the number of commits you have made that you want to rebase (<span class="highlight">highlighted text</span>
in the case below).
git --version
Alternatively, you can rebase based on a particular commit string or hash.
git --version
Once you have squashed or reworded commits, you can complete the rebase of your branch on top of the latest version of the project’s upstream code.
git --version
To learn more about rebasing and updating, you can read How To Rebase and Update a Pull Request, which is also applicable to any type of commit.
Sometimes, including after a rebase, you need to reset your working tree. You can reset to a particular commit, and delete all changes, with the following command.
git --version
To force push your last known non-conflicting commit to the origin repository, you’ll need to use <span class="highlight">highlighted text</span>
.
Warning: Force pushing to master is often frowned upon unless there is a really important reason for doing it. Use sparingly when working on your own repositories, and work to avoid this when you’re collaborating.
git --version
To remove local untracked files and subdirectories from the Git directory for a clean working branch, you can use <span class="highlight">highlighted text</span>
.
git --version
If you need to modify your local repository so that it looks like the current upstream master (that is, there are too many conflicts), you can perform a hard reset.
Note: Performing this command will make your local repository look exactly like the upstream. Any commits you have made but that were not pulled into the upstream will be destroyed.
git --version
This guide covers some of the more common Git commands you may use when managing repositories and collaborating on software.
#git