I selfishly created this cheat sheet for myself after trying to learn git for the third time. I figure that in the future, if I need help remembering what I spent several painstaking hours to learn it would be in my best interest to document my findings. Hopefully, someone else out there finds this useful as well. 🤘
For a tutorial, a buddy of mine recommended this one. Let's just say I trust this man with my life, so you can at least trust his judgment.
1. Download git and choose your version control software. Grab the latest Git distribution from their website. If you're on MacOS, you can use brew to download packages to your machine like I did. I am not here to recreate the wheel. A simple search on the web will return hundreds of resources that will walk you through git installation. Again, the way you do it will be up to you!
One of the beautiful things about Git is that you can either use it as a standalone package or you may use it in tandem with a variety of version control graphical clients. Choose your flavor. I favor the Git/Github variety in my day-to-day.
2. Configure Git to Track Your Changes.
Configuring Git with your credentials allows Git to begin tracking changes made by you. That way, when changes are committed, they are added to a ledger of all updates made to a repository and linked to you. With git log
and git reflog
, you can see the who, when, and what regarding each change that was committed.
git config --global user.name "full name"
git config --global user.email "email"
git config --global core.excludesfile [file]
3. Make it look pretty ✨ (optional but worth it). The following command will enable the syntax coloring for git.
git config --global color.ui true
If you're using zsh
and you would like to turn off the default branch listing used by git, enter the following command.
git config --global pager.branch false
NOTE: If you choose to keep the default branch listing, then you can exit by typing in -F
then return
Start using git to start tracking a local repository and connect it to a remote repository in the future.
Start watching a new repo in an already existing directory
NOTE: Execute this command once you have used cd
to navigate into the directory.
git init
Create git repo in current directory
git init <directory>
Clone a git repo to your local machine.
git clone <GitHub-url> --- clone a repo to your local machine
Display all modified files in the working directory that are staged for the next commit.
git status
Start working on a copy of your code so you can merge the changes later.
List all branches (an "*" appears next to the current)
git branch
List both remote-tracking branches and local branches
NOTE: You can combine with --list
to match optional pattern(s)
git branch -a
List or delete (if used with -d
) the remote-tracking branches. Combine with the flag --list
to match the optional pattern(s)
git branch -r
Create a new branch at the current commit
git branch <branch-name>
Switch to and create a new branch out of the working directory.
git checkout <branch-name>
Allows you to stage the changes that you just made so you update the repo.
Add a file to the repo
git add <file-name>
Add all new files in the current directory to repo
git add .
Stage changes in hunks, interactively from the command line
git add -p
# YOUR CODE HERE
# (x/x) Stage this hunk [y,n,q,a,d,e,?]?
git add -p <filename>
# YOUR CODE HERE
# (x/x) Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?
Possible Options:
y
stage this hunk for the next commitn
do not stage this hunk for the next commitq
quit; do not stage this hunk or any of the remaining hunksa
stage this hunk and all later hunks in the filed
do not stage this hunk or any of the later hunks in the fileg
select a hunk to go to/
search for a hunk matching the given regexj
leave this hunk undecided, see next undecided hunkJ
leave this hunk undecided, see next hunkk
leave this hunk undecided, see previous undecided hunkK
leave this hunk undecided, see the previous hunks
split the current hunk into smaller hunkse
manually edit the current hunk -- NOTE: Edit the hunk manually by replacing+
/-
by#
?
print hunk help
Let's save those changes, baby!
Commit changes to the branch with an inline message instead of opening the terminal editor
git commit -m "message goes here"
Edit the commit message on your most recent commit to a branch (WARNING: I suggest using this if the original message was an inline message, not a multiline message)
git commit --amend -m "New message goes here"
Let's bridge the gap between the local repos and the ones that you have on Github.
Display all of the git remotes for the repo
git remote
Display the status of all remotes for the repo
git remote -v
Add an alias to a remote repository
git remote add <alias> <GitHub-url>
A list of branches associated with the remote and also the endpoints attached for fetching and pushing
git remote show <alias>
Remove the connection to the remote repository called
git remote rename <old-name> <new-name>
Changes the list of branches tracked by the named remote
git remote set-branches --add <name> <branch>
Gives you the power to make the changes that you have made locally available elsewhere. Transmit commits to the current branch
git push -u <GitHub-url>
Add a new branch created locally to the remote repository
git push --set-upstream <alias> <branch-name>
Now, you can use git push
to send those changes to the remote
git push <alias> <branch>
Keep yourself (and your branch) up to speed and copy the changes.
Fetch the specified remote’s copy of the current branch and immediately copy it into the local repository
git pull <remote>
Allows you to check to see if you're up-to-date.
Retrieve changes from upstream from the Git remote without copying
git fetch <alias> ---
Fetches a specific <branch>
, from the repo. Leave off
git fetch <remote> <branch>
Allows you to combine branches that you were working on.
Merge the current branch with the one indicated in the call
git merge <branch-name>
Merge a remote branch into the active branch to bring it up to date
git merge <alias>/<branch-name>
Compare differences in code that you've made changes to and what has been committed.
Display what has been updated but not added
git diff
Display what is updated but not staged
git diff --stages
Show the difference between the working directory and the last commit
git diff HEAD
Show the difference between staged changes and the last commit
git diff --cached
Made a mistake? Have no fear; not all hope is lost (yet). The beauty of version control is that you can track all of your past versions and retrieve lines of code or entire files. This can get really advanced, really fast. I suggest fixing your mistakes under the guidance and supervision of someone else who knows their way around Git. I'm speaking from experience and guaranteeing it will save you 50 hours of headaches.
Do some spring cleaning and get rid of some files or move them around then commit them. Using this method, you can change the file structure or delete things from your project with the safety net of being able to roll back your changes with Git.
Delete a file from the project
git rm <file-name> --- and stage the removal for a commit
Change an existing file path
git mv <exisitng-path> <new-path> --- and stage the move for a commit
This command and all of its variations are useful commands that let you display information about your branches.
Show all commits in the active branch
git log
Display the commit logs, including any paths that may have been removed
git log --stat -M
Roll back your changes to a previous version of your work. Un-stage a file but retain the current changes in the working directory before a commit
git reset <file-name>
Reset a branch using a previous commit ID (find this using git reflog
)
git reset --hard [commit]
Create a new commit that undoes all of the changes made in , then apply it to the current branch
git revert <commit>
Remove from the staging area, but leave the working directory unchanged
git reset <file>
NOTE: This will unstage a file without overwriting any changes.
Shows which files would be removed from the working directory.
NOTE: Use the -f
flag in place of the -n
flag to execute the clean
# Run a test run
git clean -n
# Run the clean
git clean -f
Git is a powerful tool because it allows us to code collaboratively with other people! Let's follow the steps to set up your local machine for collaboration. Before we get into the commands, fork a repository so you have a copy to work from (tutorial). Then, clone the repository using the command line on your local machine. Tada! Now you have a local copy and you can start making changes.
git remote add upstream <repo-URL>
git fetch upstream
So now you know the basics, what the heck can you actually do with all you've just learned? Let me walk you through some common workflows.
- Create a branch ---
git branch <new-branch-name>
- Check the current remotes ---
git remote -v
- Add a remote ---
git remote add <alias> <GitHub-url>
- Get the new remote branches ---
git fetch <alias>
- Finalize the remote by pushing the changes ---
git push <alias> <Github-url>
git remote rm <alias>
- Add specific files to the repo OR add all of the changes to the repo ---
git add [file/s]
ORgit add .
- Commit and add a short descriptive message ---
git commit -m "Meaningful text goes here"
- Send the changes from your local repo to the remote ---
git push <alias> <branch-name>
orgit push -u <alias>
- Workflow: Add a remote
- Workflow: Delete Remote
- Workflow: Push Changes to GitHub
- Workflow: Add a repository
- Workflow: Update your fork with changes from the original repo
- Workflow: Add a branch
- Workflow: Merge branches
- Quick tips
- Helpful videos
- Vocabulary (alias, branch, commit, remote, local, repo)
- Useful tags
This is a resource not only for me but for you! If there is anything else that you think should be in here let me know. Feel free to raise an issue.
Once you've mastered the basics of git yourself, you can learn how to transfer the knowledge into your workflow. I highly recommend this team toolkit that was created by my friend @emiton.