Git Commands Adding, Committing and Branching Cheatsheet

Here are a list of Git commands I want to remember for myself, mainly but I highly recommend that you bookmark this for future reference. Email yourself and friends the URL if you have to. I’ve come back to it multiple times myself. :)

The following allows you to store your name and email address and have them be associated to each commit message.

git config --global user.name "YourFirstName YourLastName"
git config --global user.email 'YourEmail@domain.com'
git config --list
cat .gitconfig
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.lc log ORIG_HEAD.. --stat --no-merges (if you want to list all commits after you fetched but exclude merges)

This creates the .git directory which contains all of the Git files, directories and hooks. Hooks can be executed after certain events. More on hooks maybe later.

cd [root of project directory]
git init (creates a .git directory)

The patterns of files to exclude should be saved in the .gitignore file. This file will be checked into Git and is part of the project. So clones will get the .gitignore file.

create a .gitignore file in root of project directory with the following contents:
*.log

The .git/info/exclude file is your personal copy which doesn't get committed into Git.

However, this doesn’t commit to local repository yet. You must commit the changes. See below.

git add .
git status (tells you which files have been added and ready to be committed)

A git add FILENAME/DIRNAME will add files/directories to the index.
If for some reason you’d like to undo the git add, do the following:

git reset FILENAME/DIRNAME
git status (to confirm your files now show under the 'Untracked files' section)

* Be careful with the following as it will set all of your local changes to HEAD version of master branch.

Change ‘master’ to another branch name if you want your local branch to be the same as the same branch in the origin repo.

git fetch
git reset --hard origin/master

* Be careful with the following as it will revert all of your local changes to HEAD version.

git status (these are the files which will be reverted when executing the following)
git checkout -f

* Be careful with the following as it will revert all of your local changes to HEAD version.

git status (these are the files which will be reverted when executing the following)
git clean -fd
git pull

See below to see how to commit files subsequent times.

git status (tells you which files have been added and ready to be committed)
git commit -m "First Commit"
git ls-files
git rm [filename]

* This can only be done after you have committed and prior to pushing changes to remote repository.

git commit --amend

git commit -a is the same as doing a git add [file name] followed by git commit -m “your commit message”
The git commit -a option will automatically include all files that need to be added to the commit.

git status (tells you which files have been added and ready to be committed)
git commit -a
Then edit notes in the file and save it.

git commit -m "your description of what was changed" -a (you can do this instead of git commit -a. This will prevent the editor from being opened though)
git commit -v
git log
git log -- filename
gitk filename

OR

git log -p filename

If you receive an error saying “Error reading commits:fatal:unrecognized arguments: -all”, it’s because the all flag option requires two hyphens. See below:

gitk --all

The name of the colored branch indicates the branch you are working on currently.

git branch --color
git branch -a

You can first get a list of branches as described above and then do the following:

git branch -d [branch_name]

Beware to make sure this is really what you want to do!

You can first get a list of remote branches as described above and then execute the command below.
* Be sure the branch name is preceded with a colon. Notice it below.

git push origin :branch_name

It’s important to remember that if you add a file in one branch, the file won’t exist in another branch, unless you add it or merge it.

git branch [branch name]
git  checkout [branch name]
git co [branch name] (you can use co if you've set up the alias co above)
git branch --color
git branch -m old_branch new_branch

Caution: This will overwrite the existing branch!

git branch -M old_branch existing_branch_to_overwrite

When you create a branch in your local repo, it’s not automatically added to the remote repo; You need to push it there.

git push origin [branch name]
git push origin '*:*'
git fetch origin
git checkout --track origin/[branch_name]

For example, you may want to clone an existing repository that’s located on the same server as where you want to create the new clone. OR you may want to create the new clone on your local computer from the source repository which is on a shared hosting account.

git clone /source/repository/path /destination/repository/path (assumes the source repository is on the same server as where you want to clone)

OR

cd [to_a_directory_above_where_you_want_the_project_files_to_go]
git clone username@yourdomain.com/~/git/yourproject.git (to clone a remote git repository)
cd this_repository_directory (one directory above .git)
git pull /the/other/clone HEAD

In this example, I’ll show you how to do a merge, after a conflict, between code of two branches. This is the case where Git couldn’t auto-merge.

git checkout master (switches you to the master branch)

vi testfile (add the following line in testfile and save)
adfasfasdfadf
asdf
a
sdf
asdf
asdfasdf;lkja;kladjsf;klj

git add testfile (allows it to be tracked by Git)

git commit -m "adding content for master branch" -a

git branch test (create the test branch)

git checkout test (switches you to the test branch)

vi testfile (delete all of the existing content and make your testfile in the test branch look like this:)
;lkajsdf;lkajdsl;kajsdf
;lkajsdf;ladjsl;kadjsf
asdf
asd
fasd
fadsf

git commit -m "adding content for test branch" -a

git merge master (merge contents of this test branch with the master branch. see below)
your_username$ git merge master
Auto-merged testfile
CONFLICT (content): Merge conflict in testfile
Automatic merge failed; fix conflicts and then commit the result.

vi testfile (edit this file, resolve the conflicts by getting rid of the <<<<<<< HEAD:testfileD:testfile, ======= and >>>>>>> master:testfile and make the file look like how it should)

Assume the file's contents should look like this:

;lkajsdf;lkajdsl;kajsdf
;lkajsdf;ladjsl;kadjsf
asdf
asd
fasd
fadsf
asdfasdf;lkja;kladjsf;klj

git commit -m "adding content for master branch" -a (to commit your changes)

If you’d like to set up a Git repository on a shared hosting account, see: Setting up Private Git Repository on Shared Hosting Server, BlueHost

VN:F [1.9.22_1171]
Rating: 4.7/5 (9 votes cast)
VN:F [1.9.22_1171]
Rating: +5 (from 5 votes)
Git Commands Adding, Committing and Branching Cheatsheet, 4.7 out of 5 based on 9 ratings
Facebook Twitter Email

4 Comments to “Git Commands Adding, Committing and Branching Cheatsheet”