— Technology — 2 min read
Configure user information for all local repositories
1git config --global user.name "[name]"
Sets the name you want attached to your commit transactions
1git config --global user.email "[email address]"
Sets the email you want attached to your commit transactions
1git config --global color.ui auto
Enables helpful colorization of command line output
Branches are an important part of working with Git. Any commits you make will be made on the branch you’re currently “checked out” to. Use git status
to see which branch that is.
1git branch [branch-name]
Creates a new branch
1git checkout [branch-name]
Switches to the specified branch and updates the working directory
1git merge [branch]
Combines the specified branch’s history into the current branch. This is usually done in pull requests, but is an important Git operation.
1git branch -d [branch-name]
Deletes the specified branch
A new repository can either be created locally, or an existing repository can be cloned. When a repository was initialized locally, you have to push it to GitHub afterwards.
1git init
The git init
command turns an existing directory into a new Git repository inside the folder you are running this command. After using the git init
command, link the local repository to an empty GitHub repository using the following command:
1git remote add origin [url]
Specifies the remote repository for your local repository. The url points to a repository on GitHub.
1git clone [url]
Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits
Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore
. You can find helpful templates for .gitignore
files at github.com/github/gitignore.
Synchronize your local repository with the remote repository on GitHub.com
1git fetch
Downloads all history from the remote tracking branches
1git merge
Combines remote tracking branches into current local branch
1git push
Uploads all local branch commits to GitHub
1git pull
Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull
is a combination of git fetch
and git merge
Browse and inspect the evolution of project files
1git log
Lists version history for the current branch
1git log --follow [file]
Lists version history for a file, beyond renames (works only for a single file)
1git diff [first-branch]...[second-branch]
Shows content differences between two branches
1git show [commit]
Outputs metadata and content changes of the specified commit
1git add [file]
Snapshots the file in preparation for versioning
1git commit -m "[descriptive message]"
Records file snapshots permanently in version history
Erase mistakes and craft replacement history
1git reset [commit]
Undoes all commits after [commit], preserving changes locally
1git reset --hard [commit]
Discards all history and changes back to the specified commit
⚠️CAUTION! Changing history can have nasty side effects. If you need to change commits that exist on GitHub (the remote), proceed with caution. If you need help, reach out at [github.community]() or contact support.