Skip to main content

3 posts tagged with "git"

View All Tags

· One min read
Lejen

Step 1: Pull upstream changes

git pull --rebase upstream master

The --rebase option places your changes on top of the latest commit without merges.

Step 2: (Optional) Merge your commits into 1 commit

git reset --soft upstream/master

This command will "undo" all your commits, but won't change the files. So you can commit all your changes in a single commit.

git commit -a

Step 3: Check & test your changes

To show the changes use a GUI like the built-in gitk, Sourcetree, TortoiseGit or Tower (paid), etc.

Step 4: Push

git push will throw an error, push with "-f"

git push -f origin master

Additional information

The command to add a remote is:

git remote add upstream git://github.com/[username]/[project].git You can also also pull from a direct URL:

git pull --rebase git://github.com/[username]/[project].git But then you'll need the hash of the latest upstream commit instead of "upstream/master" in the other steps.

· One min read
Lejen

Since the task was to simply use another branch instead of master, you can simply remove master branch completely or rename it to let's say - legacy, then take another branch and rename it to master. That's it. Here are actual commands that you might need to execute to achieve the goal locally and on GitHub:

git branch -m master legacy               # rename local master to legacy
git checkout legacy
git branch -m another_branch master # another_branch will be our new master

Locally we are done now. However you cannot simply remove master branch on GitHub. You need to take another branch as default first. This can be done in repository Settings > Default Branch. Once you do this, you can proceed:

git push origin :master                   # remove master on GitHub
git push origin master # push out our new master branch
git push origin legacy # push our legacy branch too

Then go back to Settings > Default Branch and switch default branch back to master. Additionally you can remove all extra branches that you might have created during migration process.

Alternatively, if you are looking to save all your actions in history, check a correct answer here.

· 3 min read
Lejen

The git commit --amend command allows you to change the most recent commit message.

Not pushed commit

To change the message of the most recent commit that has not been pushed to the remote repository, commit it again using the --amend flag.

  1. Navigate to the repository directory in your terminal.

  2. Run the following command to amend (change) the message of the latest commit:

git commit --amend -m "New commit message."

What the command does is overwriting the most recent commit with the new one.

The -m option allows you to write the new message on the command line without opening an editor session.

Before changing the commit message, you can also add other changes you previously forgot:

git add .

Pushed commit

The amended (changed) commit is a new entity with a different SHA-1. The previous commit will no longer exist in the current branch.

Generally, you should avoid amending a commit that is already pushed as it may cause issues to people who based their work on this commit. It is a good idea to consult your fellow developers before changing a pushed commit.

If you changed the message of the most recently pushed commit, you would have to force push it.

  1. Navigate to the repository.

  2. Amend the message of the latest pushed commit:

git commit --amend -m "New commit message."
  1. Force push to update the history of the remote repository:
git push --force branch-name

Changing an Older or Multiple Commits

If you need to change the message of an older or multiple commits, you can use an interactive git rebase to change one or more older commits.

The rebase command rewrites the commit history, and it is strongly discouraged to rebase commits that are already pushed to the remote Git repository.

  1. Navigate to the repository containing the commit message you want to change.

  2. Type git rebase -i HEAD~N, where N is the number of commits to perform a rebase on. For example, if you want to change the 4th and the 5th latest commits, you would type:

git rebase -i HEAD~5

The command will display the latest X commits in your default text editor:

pick 43f8707f9 fix: update dependency json5 to ^2.1.1
pick cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
pick c5e078656 chore: update dependency flow-bin to ^0.109.0
pick 11ce0ab34 fix: Fix spelling.

# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)
  1. Move to the lines of the commit message you want to change and replace pick with reword:
reword 43f8707f9 fix: update dependency json5 to ^2.1.1
reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
pick c5e078656 chore: update dependency flow-bin to ^0.109.0
pick 11ce0ab34 fix: Fix spelling.

# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)
  1. Save the changes and close the editor.

  2. For each chosen commit, a new text editor window will open. Change the commit message, save the file, and close the editor.

fix: update dependency json5 to ^2.1.1
  1. Force push the changes to the remote repository:
git push --force branch-name

Conclusion

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N.

Don’t amend pushed commits as it may potentially cause a lot of problems to your colleagues.