Simple Git Command Reference

Git Clean

Git Clean - Removes untracked files from your working directory

  • This is really more of a convenience command, since it’s trivial to see which files are untracked with git status and remove them manually. Like an ordinary rm command, git clean is not undoable, so make sure you really want to delete the untracked files before you run it.

Git Rebase

Git Rebase - Forces git to push everything to master

git rebase <remote/repo/branch>

Git Remote

Git Remote - Add a remote repo

git remote add [alias] [url]
git remote add origin  

view all remotes and their addresses

git remote -v  

delete a remote repo

git remote rm origin  

git remote rm <destination>

Git Show

Git Show - Show code changes.

Git Add

Git Add - Add files to the GIT index to be tracked. 

  • Add all files in the directory
git add 

git add <filename> - add file to files being tracked by git

  • Only need to add once. After that file will always be added to .git local repo

git add -u - looks at all the currently tracked files and stages the changes to those files if they are different or if they have been removed.

  • It does not add any new files, it only stages changes to already tracked files.

git add -A

  • same as git add
git add -u   

git rm --<filename>

  • remove an added file

Git Pull

Git Pull - pull all files from the repo

git pull origin master  

git pull --rebase origin master

Example, pull all files from the master branch of a repo on github

sudo git pull <reponame> master  

FORCE pull everything

  • Pull just one file - git pull <file>
  • See fetch section.

Git Push

Git Push - Push changes to the remote repository

git push origin master  

    Example: When the repository is named repo1

    sudo git remote -v
    sudo git push repo1 master  
    • Check name of repo

    git remote (tell git what the name of the remote server is)

    git remote add nameOFremote path
    git remote rm nameOFremote
    remove a remote repo

      git push nameOFremote branch

      • pushes changes to remote repo
      • ex.git push test master   (remote repo = test, master branch)

      Options for git push

      • -u link local repo with remote repo

      If you cannot connect (permission error)

      • try command ssh -vT
      • this will show if keys are being found
      • sudo uses a different key than the current user so connection may fail if you are using sudo. In almost all cases you should not be using the sudo command with git. If you have a very good reason you must use sudo, then ensure you are using it with every command (it's probably just better to use su to get a shell as root at that point). If you generate ssh keys without sudo, then when you try to use a command like sudo git push, you won't be using the ssh key you generated.
      • Compare the key fingerprints to github
        • ssh-keygen -lf ~/.ssh/

      list files to be pushed

      git diff —stat origin master

        force a push to the remote repo

        git push -f

          git push from server example

          • Sometime we get lazy, fix something directly on the server and push it back to the repo:
          git add file.ext
          git commit -m ‘fixed css margins’ (notice don’t need filename for the commit statement, don’t need origin master for the push statement)
          git push


            HELP! In a JAM!

            • Ops, pulled down a branch and now there’s a problem:
            • Untracked working tree file ….. would be overwritten by merge
            • GIT help: git help (command you want help with)
            • Key install in wrong place
            • error: cannot open .git/FETCH_HEAD: Permission denied
              Change owner of .git directory to yourself. 
            • Merge Conflict in GITHUB
              • 1) Delete the current file from the target (ex. master branch)
              • 2) Move the new file manually to the target.
              • 3) in the target (ex. live, master branch), commit the new file and push to github.
              • 4) try to merge again.
              • SEE notes for MERGE above (use “theirs” and “ours”)
                • git merge -X theirs origin/core_7_64
            • GITHUB  Can’t automatically merge
              • non-fast-forward updates were rejected
                Merge the remote changes (e.g. 'git pull') before pushing again.
                • This means that there are files/changes on the target server (remote) that are not on the source server (local). Therefore, perform a pull to get those files on the local server. Then, retry the push. 
            • To prevent you from losing history, non-fast-forward updates were rejected

              Merge the remote changes (e.g. 'git pull') before pushing again.
              • Execute git pull origin master, resolve any conflicts, and then re-push
              • Execute git push -f origin master. This will totally overwrite the history in your remote repository, so DO NOT do this unless you're absolutely sure.


            local repository is corrupted
            master and development are entirely different commit histories.
            • UNDO
            ERROR: Repository not found.
            fatal: Could not read from remote repository.
            • check with ssh -vT, make sure Hi repoAcct/repName is correct. 
              • If repoName is not account, check your deploy keys. 
            • See Cloud security Notes
            • use sudo
            • use the HTTPS URL instead of the SSH/git URL to avoid having to deal with SSH keys
            The following untracked working tree files would be overwritten by checkout
            error: Your local changes to the following files would be overwritten by merge: Please, commit your changes or stash them before you can merge.
            use “ git stash” to stash (store the changes) of the file in question then do the pull again. 
            • Try git stash (see above)
            • Possible fix:
              • git fetch —all     (— is two dashes)
              • git merge origin/master
              • ** if still get error, delete the files (git will reinstall them on merge)
              • git merge origin/master
            • git reset --hard origin/master     This will force the fetched master to overwrite the local files. 
            error: unable to unlink old
            Git pull results in extraneous “Merge branch” messages in commit log
            fatal: Unable to mark file xx/xx/xx/xx
            ERROR: Repository not found.
            fatal: Could not read from remote repository.
            error: unable to unlink old 'sites/default/default.settings.php' (Permission denied)
            fatal: Cannot do hard reset with paths.
            • GIT pull request pulls in old commits
            CONFLICT (modify/delete): file2.txt deleted in HEAD and modified in 
            Automatic merge failed; fix conflicts and then commit the result.
            • try git commit -a
            HEAD detached at XXXXXXX

            Git Diagrams

            In software development, Git is a distributed revision control and source code management (SCM) system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005.

            Every Git working directory is a full-fledged repository with complete history and full version tracking capabilities, not dependent on network access or a central server.

            Git is free software distributed under the terms of the GNU General Public License version 2.


            Git Init

            Git Init - Initialize the Repo

            • Initialize from WITHIN the directory that want to work. just type "git init"
            • bare

            Git Fetch

            Git Fetch <Remote/Repo Name> - The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet.

            git fetch <Remote/Repo>

            • ex. git fetch origin master (does not change local working copy)

            git diff master origin/master -  Check the difference between your local copy of the master branch and the files that you fetched

            git fetch --all -  FORCE local to pull down remote files and overwrite all local files ( ** two dashes before all)

            git fetch —all

            Git pull just one file

            git fetch
            git checkout -m <revision> <yourfilepath>
            git add <yourfilepath>
            git commit 


              Check difference between workspace (Working Directory) and Index

              git diff (will show actual file content changes)

                Check difference between working directory and local repo

                git diff HEAD (will show actual file content changes)

                  git log

                  get commits
                  git diff (commit number)

                    git merge

                    • Merge you local repo with your local files (workspace). After fetching, you'll need to use merge to actually update your local files.
                      git merge origin/master (Here the origin = remote repo)

                      Check the difference between your local copy of the master branch and the files that you fetched

                      git diff master origin/master

                        Git Commit

                        Git Commit - Commit all changes to your local repo

                        • GIT keeps it own copy of all the files that have been added to GIT using git add . When a change is made to a file, that change must be added to GIT's version of the files (local repo). This is done with a commit.
                        • Add comment to file 
                        • Each commit is stored as a hash (view with GIT log)
                        • files changed will show as 'modified: " under git status

                        commit one file

                        git commit path/filename

                          git reset

                          • remove file from staging area

                          git reset --hard

                          • put all changes back to last commit. Reverts changes of actual file. Be careful!

                          commit -a

                          • commit all changes, don't use staging area

                          commit -m

                          sudo git commit -m 'removing jrtest.txt'(commits are LOCAL)
                          • add message

                          Delete a commit

                          • git reset --soft HEAD~1
                            • This will revert the last commit.
                            • run it again to get the second to last commit, or change the “1” to the number of commits you want to delete. ex, 3 for the last 3 commits. 

                          Git Clone

                          git clone <url>

                          • copy an entire project to local

                          To only get the most recent version of the files, suppress the default checkout of all files with

                          • git clone -n git://path/to/the_repo.git --depth 1
                            • depth 1 option means get only most recent version.  

                          Put files in correct directory

                          • ~~~  ./directory  
                          • Or add the directory name to end of statement and it will put all files in that directory.   *The directory should not already exist. Run the command from the parent of the target directory. The user running this command must have permissions to create directories (without sudo)
                            git clone ./directory

                            MAC issues

                            • Possibly will show files as deleted. Use git status to check.
                            • use git reset --hard to reset status of files. 

                            Git Checkout

                            Git Checkout - Checkout a branch or paths to the working tree

                            use "git checkout -- ..." to discard changes in working directory)

                            checkout a branch so others can't edit it

                            checkout a file on a remote branch

                            git checkout remote/branch path/to/file
                            git checkout some_remote/branch32 conf/en/myscript.conf
                            git checkout some_remote/branch1 conf/fr/load.wav

                              Checkout a file on a remote branch to update the local repo

                              git checkout <commit>
                              git checkout c2dd73f

                                list all files being tracked by git

                                git ls-files

                                  list all files for a specific branch

                                  git ls-tree -r master --name-only
                                  git ls-tree -r HEAD --name-only

                                    Git Ignore

                                    Git Ignore - tell git to ignore some files

                                    • ex. a settings file that only relates to the production server
                                    • Create the file .gitignore
                                    • add files to be ignored to this file
                                    • remove them from git for being tracked
                                      git rm --cached <file>

                                      Reset Head

                                      • (! careful with this one !) git reset HEAD [filename]
                                        • make sure to include the path to the file
                                          git reset HEAD sites/default/files/sites/all/default/files/video/filename.mp4
                                        • remove a file that has already been committed.




                                        These helpful git commands should be handy in keeping your repo happy.

                                        Our team is happy to answer your sales questions. Fill out the form and we’ll be in touch as soon as possible.


                                        2901 E Baltimore St, Baltimore, MD 21224