Table of Contents
1 Commonly used Git commands
- git add <file>
- Add a file to the staging area
- git checkout -b <branch name>
- Creates a new branch and switches to it.
- git commit -m <message>
- Commits the files in the staging area to the repository
- git commit –amend
- Simply replaces the last commit with a new one, including files in the staging area with the latest commit. This is useful if files were missing from the previous commit.
- git commit –amend -C HEAD
- Adds a file to the most recent commit
- git commit –amend -m <new message>
- Fixes the message of the last commit.
- git config –list
- Lists parameters from the current configuration, such as the user name, e-mail address, and default text editor.
- git diff
- Show all differences between unstaged files and files in the working directory.
- git diff –staged
- Show all differences between staged files and the current working directory.
- git fetch <remote name>
- Get all data that you don't yet have from a remote repository. Merges must be manually performed. (Use git pull to automatically merge data from a repo with your working directory.)
- git log
- Show the commit history of the project. Best used with options like -p, –stat or pretty=oneline.
- git merge <branch name>
- Merge the branch <branch name> into the current working branch. Any conflicts will be highlighted in the conflicted files for you to fix.
- git push origin master
- Pushes the repository to the repository referenced by origin to the master branch.
- git status
- Lists tracked and untracked files, if they've been changed since the last commit, etc.
- git remote -v
- View the current remotes and their names.
- git remote set-url origin <url>
- Sets the URL or a remote.
- git reset –hard HEAD~1; git push -f
- Resets a bad commit to the previous one after a push.
2 Common working tips
2.1 Create a .gitignore file
Git can automatically ignore files that follow certain specified patterns. To do this, create a file called .gitignore inside the working directory. Inside this file, add the patterns you want Git to not even bother looking at.
For example, I can ignore the Emacs backup files that end in a tilde by simplying adding
to the file. Now, files ending in tilde won't even be listed as untracked when running the git status command.