Helpful Git Commands

Snigdha Singh
4 min readMay 9, 2022

--

Basic git commands for command line including login, push commits, merging multiple commits to one, and adding .gitignore.

Photo by Roman Synkevych 🇺🇦 on Unsplash

Logging into git using terminal

You can either use your username or email id. I would recommend using your email id if you are using an enterprise account.

git config --global user.email "your_emailid@email.com"

Clone repo

git clone <repo_url>

Switching to a new branch

You can either use git checkout or switch. Personally, I prefer using switch.

Switch to a new branch:

git switch -c <new_branch_name>

Switch to an existing branch:

git switch <old_branch_name>

Add, commit and push changes to remote

After you are done making your changes, you can push the changes to remote. You can make multiple commits and keep track of all the things you changes.

git add .
git commit -m "Write your commit message specifying what change you made"

You can also combine the add and commit commands by adding -a to the commit command.

git commit -a -m "Commit message"

Push changes to remote branch:

git push origin <branch_name>

After this you can see your changes on the web interface. You can then create a pull request and try to merge your branch to master.

If someone else also pushed changes to master after you cloned the repo, you might need to pull master and resolve conflicts.

git pull origin master

Push changes to the same commit

Pushing to the same commit is not recommending as you don’t have the record of all the changes you did, but if it is a minor typo in the README file, it might be convenient to do so.

Add the file that you made changes to separately, here I am using readme as an example.

git add ../README.md

Then use the commit amend command.

git commit --amend

After this, you can check git log to confirm that your changes were added to the same commit.

Rename Git branch

git branch -m <newBranchName>

After this, you will have to push the changes. Git does not edit the name of the remote branch but pushes it as a new branch. You will have to delete the branch with the old branch. You can do both of this by running the following commands.

git push origin <newBranchName>
git push origin --delete <oldBranchName>

See git logs

Git logs are helpful to see what changes have been made. Just use git log. If you want to see an abbreviated version of git logs then use `git log --pretty=oneline --abbrev-commit . To exit git log, type “q” or “z” or type “h” to seek for help.

Merge multiple commits into one

When you are working on a big repo and you need to merge your branch into master, it might be helpful to merge all your commits into one (as the final step) so that your merge will be cleaner. This will also help code reviewers to properly access the changes you made at one glance rather than scouring all the commits.

If you know how many commits you have, and want to rebase then you can use can use the command git rebase -I HEAD~<number of commits to merge>. Example of this merging three commits would be:

git rebase -i HEAD~3

If the number of commits is large, you can use the commit hash or SHA to rebase onto that particular commit. You can either use the 7 digit short commit hash or the default value.

git rebase -i <commit_hash_value>

After this git will open a list of all the commits.

pick 03fxxxx solving yaml file syntax errors
pick 1b3xxxx adding aws deploy step
pick dc3xxxx adding zip file step to push to aws
pick xxxx603 adding test function
pick xxxx890 error fix
pick xxxx052 error fix
pick cbx7xxx error fix

You need to pick one of these commits, and change all the others to squash or ‘s’.

pick 03fxxxx solving yaml file syntax errors
squash 1b3xxxx adding aws deploy step
s dc3xxxx adding zip file step to push to aws
s xxxx603 adding test function
s xxxx890 error fix
s xxxx052 error fix
s cbx7xxx error fix

If the list of the commits is very long, use control(^) + V and -> to select the word pick and then use the down arrow to select all the rows where you need to replace the word. Press d to delete the word ‘pick’ from all the rows. Go to the top row, press control(^) + V and press down arrow to select all rows where you want to insert the word. Press Shift + I and type the word ‘squash’ and then press esc .

Commit and push the changes.

Adding .gitignore

You can have a .gitignore file on the main directory or in each of the folder with the respective files. If your file is checked in a commit, adding it to .gitignore file later will not delete it. You will need to manually delete the file.

git rm --cached <file_name>

This only removes the file from the directory that you are in, to remove it from all the folders, use:

find . -name <file_name> -print0 | xargs -0 git rm --ignore-unmatch

Make a .gitignore file and add the names of the files with path that you don’t want in your directory.

touch .gitignore
echo <file_name> >> ~/.gitignore

If you want to ignore the file from all the folders. Use:

echo **/.DS_Store >> ~/.gitignore

--

--

Snigdha Singh

I am a Software Developer with a Master’s in Computer Science and Software Engineering. I work with Java, Python, AWS.