Git Cheat book – The Common Git commands
An article briefly listing the common git commands that will come handy in some common use cases.
As we know, Git is a free and open-source distributed version controlling system, which helps to keep track of our projects. It does not matter whether it is a small project or a very huge project with thousands of people contributing. Git comes useful in all scenarios. In this article, we’ll briefly list some of the common use cases of Git. It is not going to a complete manual for the Git tool. But, it will help us easily find the solution that we are looking for.
The Basic Commands
Configuring user information used in all local repositories
git config --global user.name “[firstname lastname]” git config --global user.email “[valid-email]”
If you want to set name and email for a specific repository, then
cd in to the repo folder, and run above commands by omitting
Creating, Cloning a project.
Create a git repository for an existing project
cd [project folder] git init
Link a local repository with a remote repository.
git remote add origin [remote-repo-url]
Stage, Commit & Push
Staging the changes to made in local repository. Staging means, adding the candidate files to commit.
# Stage all files in the working directory, recursively git add . # Stage all files matching with the given pattern. git add [pattern]
Removing files from staged index.
#unstage all files without deleting anything. git reset # Remove specific staged file git reset [file pattern] # Remove uncommitted, staged file. git rm --cached [file pattern]
Committing the changes in repository.
# Commit staged files. Open default editor for commit message git commit # Commit staged files. Provide commit message as parameter git commit -m "commit message" # Stage all files, and provide commit message as parameter git commit -am "commit message"
Pushing the committed changes to remote.
# Push to default remote branch git push # Commit to specific remote branch git push origin [remote branch]
Git branches are used for working on a new feature. A feature branch will be created from the current stable branch, then we’ll make the changes, deploy and verify, finally the feature branch will be merged with stable branch.
Creating a new branch
# Create a new branch from the currently active branch. git branch [new branch name] # Create a new branch and checkout that branch git checkout -b [new branch name]
Switching to a branch.
git checkout [branch name]
Checking out a remote branch.
git fetch git checkout [branch name] # The above solution won't work if there are multiple remotes. # In that case, use
git checkout -b test <name of remote>/test# or the shorthand
git checkout -t <name of remote>/test
Delete a branch.
When deleting a branch, ensure it is not the active branch
git branch -d [branch-name] # Forcefully delete branch git branch -D [branch name]
Rebasing in Git is the process of switching the base branch or taking the latest changes from the base branch. It may be necessary when you’re going to merge your branch with another branch.
git rebase [the base branch] # In case of conflicts, you might have to resolve the conflicts manually, commit the changes, and then proceed/skip/abort with below commands. git rebase (--continue | --skip | --abort ]
Once the activity on the feature branch done, we might need to merge it with the master branch.
git checkout [target branch] git merge [source branch] # The above command will merge and commit changes. # To avoid committing git merge --no-commit [source branch] # Don't create a merge commit (default) git commit --ff [source branch] # Create merge commit git commit --no-ff [source branch]
Common use cases
Clean up all local changes in a branch.
Sometimes, when you mess up with branch, you might want to cleanup all local changes a branch and start fresh.
# Revert all the modifications git checkout . # Remove staged changes git reset --hard # Remove untracked files git clean -f # Remove untracked files include directories git clean -fd
- Git cheat sheet by Github Education.
Can you remember any scenario which is not listed above? Please let us know, so we can include our cheat book :).